website.class.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. <?php
  2. /* Copyright (C) 2007-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file website/website.class.php
  23. * \ingroup website
  24. * \brief File for the CRUD class of website (Create/Read/Update/Delete)
  25. */
  26. // Put here all includes required by your class file
  27. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  28. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  29. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  30. /**
  31. * Class Website
  32. */
  33. class Website extends CommonObject
  34. {
  35. /**
  36. * @var string Id to identify managed objects
  37. */
  38. public $element = 'website';
  39. /**
  40. * @var string Name of table without prefix where object is stored
  41. */
  42. public $table_element = 'website';
  43. /**
  44. * @var array Does website support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  45. */
  46. public $ismultientitymanaged = 1;
  47. /**
  48. * @var string String with name of icon for website. Must be the part after the 'object_' into object_myobject.png
  49. */
  50. public $picto = 'globe';
  51. /**
  52. * @var int Entity
  53. */
  54. public $entity;
  55. /**
  56. * @var string Ref
  57. */
  58. public $ref;
  59. /**
  60. * @var string description
  61. */
  62. public $description;
  63. /**
  64. * @var int Status
  65. */
  66. public $status;
  67. /**
  68. * @var mixed
  69. */
  70. public $date_creation;
  71. public $date_modification;
  72. /**
  73. * @var integer
  74. */
  75. public $fk_default_home;
  76. public $fk_user_creat;
  77. /**
  78. * @var string
  79. */
  80. public $virtualhost;
  81. /**
  82. * Constructor
  83. *
  84. * @param DoliDb $db Database handler
  85. */
  86. public function __construct(DoliDB $db)
  87. {
  88. $this->db = $db;
  89. return 1;
  90. }
  91. /**
  92. * Create object into database
  93. *
  94. * @param User $user User that creates
  95. * @param bool $notrigger false=launch triggers after, true=disable triggers
  96. *
  97. * @return int <0 if KO, Id of created object if OK
  98. */
  99. public function create(User $user, $notrigger = false)
  100. {
  101. global $conf;
  102. dol_syslog(__METHOD__, LOG_DEBUG);
  103. $error = 0;
  104. $now=dol_now();
  105. // Clean parameters
  106. if (isset($this->entity)) {
  107. $this->entity = trim($this->entity);
  108. }
  109. if (isset($this->ref)) {
  110. $this->ref = trim($this->ref);
  111. }
  112. if (isset($this->description)) {
  113. $this->description = trim($this->description);
  114. }
  115. if (isset($this->status)) {
  116. $this->status = trim($this->status);
  117. }
  118. if (empty($this->date_creation)) {
  119. $this->date_creation = $now;
  120. }
  121. if (empty($this->date_modification)) {
  122. $this->date_modification = $now;
  123. }
  124. // Check parameters
  125. if (empty($this->entity)) {
  126. $this->entity = $conf->entity;
  127. }
  128. // Insert request
  129. $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
  130. $sql.= 'entity,';
  131. $sql.= 'ref,';
  132. $sql.= 'description,';
  133. $sql.= 'status,';
  134. $sql.= 'fk_default_home,';
  135. $sql.= 'virtualhost,';
  136. $sql.= 'fk_user_creat,';
  137. $sql.= 'date_creation,';
  138. $sql.= 'tms';
  139. $sql .= ') VALUES (';
  140. $sql .= ' '.((empty($this->entity) && $this->entity != '0')?'NULL':$this->entity).',';
  141. $sql .= ' '.(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").',';
  142. $sql .= ' '.(! isset($this->description)?'NULL':"'".$this->db->escape($this->description)."'").',';
  143. $sql .= ' '.(! isset($this->status)?'1':$this->status).',';
  144. $sql .= ' '.(! isset($this->fk_default_home)?'NULL':$this->fk_default_home).',';
  145. $sql .= ' '.(! isset($this->virtualhost)?'NULL':"'".$this->db->escape($this->virtualhost)."'").",";
  146. $sql .= ' '.(! isset($this->fk_user_creat)?$user->id:$this->fk_user_creat).',';
  147. $sql .= ' '.(! isset($this->date_creation) || dol_strlen($this->date_creation)==0?'NULL':"'".$this->db->idate($this->date_creation)."'").",";
  148. $sql .= ' '.(! isset($this->date_modification) || dol_strlen($this->date_modification)==0?'NULL':"'".$this->db->idate($this->date_modification)."'");
  149. $sql .= ')';
  150. $this->db->begin();
  151. $resql = $this->db->query($sql);
  152. if (!$resql) {
  153. $error ++;
  154. $this->errors[] = 'Error ' . $this->db->lasterror();
  155. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  156. }
  157. if (!$error) {
  158. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  159. // Uncomment this and change MYOBJECT to your own tag if you
  160. // want this action to call a trigger.
  161. // if (!$notrigger) {
  162. // // Call triggers
  163. // $result = $this->call_trigger('MYOBJECT_CREATE',$user);
  164. // if ($result < 0) $error++;
  165. // // End call triggers
  166. // }
  167. }
  168. // Commit or rollback
  169. if ($error) {
  170. $this->db->rollback();
  171. return - 1 * $error;
  172. } else {
  173. $this->db->commit();
  174. return $this->id;
  175. }
  176. }
  177. /**
  178. * Load object in memory from the database
  179. *
  180. * @param int $id Id object
  181. * @param string $ref Ref
  182. * @return int <0 if KO, 0 if not found, >0 if OK
  183. */
  184. public function fetch($id, $ref = null)
  185. {
  186. dol_syslog(__METHOD__, LOG_DEBUG);
  187. $sql = 'SELECT';
  188. $sql .= ' t.rowid,';
  189. $sql .= " t.entity,";
  190. $sql .= " t.ref,";
  191. $sql .= " t.description,";
  192. $sql .= " t.status,";
  193. $sql .= " t.fk_default_home,";
  194. $sql .= " t.virtualhost,";
  195. $sql .= " t.fk_user_creat,";
  196. $sql .= " t.fk_user_modif,";
  197. $sql .= " t.date_creation,";
  198. $sql .= " t.tms as date_modification";
  199. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  200. $sql .= ' WHERE t.entity IN ('.getEntity('website').')';
  201. if (null !== $ref) {
  202. $sql .= " AND t.ref = '" . $this->db->escape($ref) . "'";
  203. } else {
  204. $sql .= ' AND t.rowid = ' . $id;
  205. }
  206. $resql = $this->db->query($sql);
  207. if ($resql) {
  208. $numrows = $this->db->num_rows($resql);
  209. if ($numrows) {
  210. $obj = $this->db->fetch_object($resql);
  211. $this->id = $obj->rowid;
  212. $this->entity = $obj->entity;
  213. $this->ref = $obj->ref;
  214. $this->description = $obj->description;
  215. $this->status = $obj->status;
  216. $this->fk_default_home = $obj->fk_default_home;
  217. $this->virtualhost = $obj->virtualhost;
  218. $this->fk_user_creat = $obj->fk_user_creat;
  219. $this->fk_user_modif = $obj->fk_user_modif;
  220. $this->date_creation = $this->db->jdate($obj->date_creation);
  221. $this->date_modification = $this->db->jdate($obj->date_modification);
  222. }
  223. $this->db->free($resql);
  224. if ($numrows > 0) {
  225. // Lines
  226. $this->fetchLines();
  227. }
  228. if ($numrows > 0) {
  229. return 1;
  230. } else {
  231. return 0;
  232. }
  233. } else {
  234. $this->errors[] = 'Error ' . $this->db->lasterror();
  235. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  236. return - 1;
  237. }
  238. }
  239. /**
  240. * Load object lines in memory from the database
  241. *
  242. * @return int <0 if KO, 0 if not found, >0 if OK
  243. */
  244. public function fetchLines()
  245. {
  246. $this->lines=array();
  247. // Load lines with object MyObjectLine
  248. return count($this->lines)?1:0;
  249. }
  250. /**
  251. * Load object in memory from the database
  252. *
  253. * @param string $sortorder Sort Order
  254. * @param string $sortfield Sort field
  255. * @param int $limit offset limit
  256. * @param int $offset offset limit
  257. * @param array $filter filter array
  258. * @param string $filtermode filter mode (AND or OR)
  259. *
  260. * @return int <0 if KO, >0 if OK
  261. */
  262. public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
  263. {
  264. dol_syslog(__METHOD__, LOG_DEBUG);
  265. $sql = 'SELECT';
  266. $sql .= ' t.rowid,';
  267. $sql .= " t.entity,";
  268. $sql .= " t.ref,";
  269. $sql .= " t.description,";
  270. $sql .= " t.status,";
  271. $sql .= " t.fk_default_home,";
  272. $sql .= " t.virtualhost,";
  273. $sql .= " t.fk_user_creat,";
  274. $sql .= " t.fk_user_modif,";
  275. $sql .= " t.date_creation,";
  276. $sql .= " t.tms as date_modification";
  277. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
  278. $sql .= ' WHERE t.entity IN ('.getEntity('website').')';
  279. // Manage filter
  280. $sqlwhere = array();
  281. if (count($filter) > 0) {
  282. foreach ($filter as $key => $value) {
  283. $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  284. }
  285. }
  286. if (count($sqlwhere) > 0) {
  287. $sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere);
  288. }
  289. if (!empty($sortfield)) {
  290. $sql .= $this->db->order($sortfield,$sortorder);
  291. }
  292. if (!empty($limit)) {
  293. $sql .= ' ' . $this->db->plimit($limit, $offset);
  294. }
  295. $this->records = array();
  296. $resql = $this->db->query($sql);
  297. if ($resql) {
  298. $num = $this->db->num_rows($resql);
  299. while ($obj = $this->db->fetch_object($resql)) {
  300. $line = new self($this->db);
  301. $line->id = $obj->rowid;
  302. $line->entity = $obj->entity;
  303. $line->ref = $obj->ref;
  304. $line->description = $obj->description;
  305. $line->status = $obj->status;
  306. $line->fk_default_home = $obj->fk_default_home;
  307. $line->virtualhost = $obj->virtualhost;
  308. $this->fk_user_creat = $obj->fk_user_creat;
  309. $this->fk_user_modif = $obj->fk_user_modif;
  310. $line->date_creation = $this->db->jdate($obj->date_creation);
  311. $line->date_modification = $this->db->jdate($obj->date_modification);
  312. $this->records[$line->id] = $line;
  313. }
  314. $this->db->free($resql);
  315. return $num;
  316. } else {
  317. $this->errors[] = 'Error ' . $this->db->lasterror();
  318. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  319. return - 1;
  320. }
  321. }
  322. /**
  323. * Update object into database
  324. *
  325. * @param User $user User that modifies
  326. * @param bool $notrigger false=launch triggers after, true=disable triggers
  327. *
  328. * @return int <0 if KO, >0 if OK
  329. */
  330. public function update(User $user, $notrigger = false)
  331. {
  332. $error = 0;
  333. dol_syslog(__METHOD__, LOG_DEBUG);
  334. // Clean parameters
  335. if (isset($this->entity)) {
  336. $this->entity = trim($this->entity);
  337. }
  338. if (isset($this->ref)) {
  339. $this->ref = trim($this->ref);
  340. }
  341. if (isset($this->description)) {
  342. $this->description = trim($this->description);
  343. }
  344. if (isset($this->status)) {
  345. $this->status = trim($this->status);
  346. }
  347. // Check parameters
  348. // Put here code to add a control on parameters values
  349. // Update request
  350. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
  351. $sql .= ' entity = '.(isset($this->entity)?$this->entity:"null").',';
  352. $sql .= ' ref = '.(isset($this->ref)?"'".$this->db->escape($this->ref)."'":"null").',';
  353. $sql .= ' description = '.(isset($this->description)?"'".$this->db->escape($this->description)."'":"null").',';
  354. $sql .= ' status = '.(isset($this->status)?$this->status:"null").',';
  355. $sql .= ' fk_default_home = '.(($this->fk_default_home > 0)?$this->fk_default_home:"null").',';
  356. $sql .= ' virtualhost = '.(($this->virtualhost != '')?"'".$this->db->escape($this->virtualhost)."'":"null").',';
  357. $sql .= ' fk_user_modif = '.(! isset($this->fk_user_modif) ? $user->id : $this->fk_user_modif).',';
  358. $sql .= ' date_creation = '.(! isset($this->date_creation) || dol_strlen($this->date_creation) != 0 ? "'".$this->db->idate($this->date_creation)."'" : 'null');
  359. $sql .= ', tms = '.(dol_strlen($this->date_modification) != 0 ? "'".$this->db->idate($this->date_modification)."'" : "'".$this->db->idate(dol_now())."'");
  360. $sql .= ' WHERE rowid=' . $this->id;
  361. $this->db->begin();
  362. $resql = $this->db->query($sql);
  363. if (!$resql) {
  364. $error ++;
  365. $this->errors[] = 'Error ' . $this->db->lasterror();
  366. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  367. }
  368. if (!$error && !$notrigger) {
  369. // Uncomment this and change MYOBJECT to your own tag if you
  370. // want this action calls a trigger.
  371. //// Call triggers
  372. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  373. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  374. //// End call triggers
  375. }
  376. // Commit or rollback
  377. if ($error) {
  378. $this->db->rollback();
  379. return - 1 * $error;
  380. } else {
  381. $this->db->commit();
  382. return 1;
  383. }
  384. }
  385. /**
  386. * Delete object in database
  387. *
  388. * @param User $user User that deletes
  389. * @param bool $notrigger false=launch triggers after, true=disable triggers
  390. *
  391. * @return int <0 if KO, >0 if OK
  392. */
  393. public function delete(User $user, $notrigger = false)
  394. {
  395. dol_syslog(__METHOD__, LOG_DEBUG);
  396. $error = 0;
  397. $this->db->begin();
  398. if (!$error) {
  399. if (!$notrigger) {
  400. // Uncomment this and change MYOBJECT to your own tag if you
  401. // want this action calls a trigger.
  402. //// Call triggers
  403. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  404. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  405. //// End call triggers
  406. }
  407. }
  408. if (!$error) {
  409. $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
  410. $sql .= ' WHERE rowid=' . $this->id;
  411. $resql = $this->db->query($sql);
  412. if (!$resql) {
  413. $error ++;
  414. $this->errors[] = 'Error ' . $this->db->lasterror();
  415. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  416. }
  417. }
  418. if (! $error && ! empty($this->ref))
  419. {
  420. global $dolibarr_main_data_root;
  421. $pathofwebsite=$dolibarr_main_data_root.'/website/'.$this->ref;
  422. dol_delete_dir_recursive($pathofwebsite);
  423. }
  424. // Commit or rollback
  425. if ($error) {
  426. $this->db->rollback();
  427. return - 1 * $error;
  428. } else {
  429. $this->db->commit();
  430. return 1;
  431. }
  432. }
  433. /**
  434. * Load an object from its id and create a new one in database.
  435. * This copy website directories, regenerate all the pages + alias pages and recreate the medias link.
  436. *
  437. * @param User $user User making the clone
  438. * @param int $fromid Id of object to clone
  439. * @param string $newref New ref
  440. * @param string $newlang New language
  441. * @return mixed New object created, <0 if KO
  442. */
  443. public function createFromClone($user, $fromid, $newref, $newlang='')
  444. {
  445. global $hookmanager, $langs;
  446. global $dolibarr_main_data_root;
  447. $now = dol_now();
  448. $error=0;
  449. dol_syslog(__METHOD__, LOG_DEBUG);
  450. $object = new self($this->db);
  451. // Check no site with ref exists
  452. if ($object->fetch(0, $newref) > 0)
  453. {
  454. $this->error='NewRefIsAlreadyUsed';
  455. return -1;
  456. }
  457. $this->db->begin();
  458. // Load source object
  459. $object->fetch($fromid);
  460. $oldidforhome=$object->fk_default_home;
  461. $pathofwebsiteold=$dolibarr_main_data_root.'/website/'.$object->ref;
  462. $pathofwebsitenew=$dolibarr_main_data_root.'/website/'.$newref;
  463. dol_delete_dir_recursive($pathofwebsitenew);
  464. $fileindex=$pathofwebsitenew.'/index.php';
  465. // Reset some properties
  466. unset($object->id);
  467. unset($object->fk_user_creat);
  468. unset($object->import_key);
  469. // Clear fields
  470. $object->ref=$newref;
  471. $object->fk_default_home=0;
  472. $object->virtualhost='';
  473. $object->date_creation = $now;
  474. $object->fk_user_creat = $user->id;
  475. // Create clone
  476. $object->context['createfromclone'] = 'createfromclone';
  477. $result = $object->create($user);
  478. if ($result < 0) {
  479. $error ++;
  480. $this->errors = $object->errors;
  481. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  482. }
  483. if (! $error)
  484. {
  485. dolCopyDir($pathofwebsiteold, $pathofwebsitenew, $conf->global->MAIN_UMASK, 0);
  486. // Check symlink to medias and restore it if ko
  487. $pathtomedias=DOL_DATA_ROOT.'/medias';
  488. $pathtomediasinwebsite=$pathofwebsitenew.'/medias';
  489. if (! is_link(dol_osencode($pathtomediasinwebsite)))
  490. {
  491. dol_syslog("Create symlink for ".$pathtomedias." into name ".$pathtomediasinwebsite);
  492. dol_mkdir(dirname($pathtomediasinwebsite)); // To be sure dir for website exists
  493. $result = symlink($pathtomedias, $pathtomediasinwebsite);
  494. }
  495. $newidforhome=0;
  496. // Duplicate pages
  497. $objectpages = new WebsitePage($this->db);
  498. $listofpages = $objectpages->fetchAll($fromid);
  499. foreach($listofpages as $pageid => $objectpageold)
  500. {
  501. // Delete old file
  502. $filetplold=$pathofwebsitenew.'/page'.$pageid.'.tpl.php';
  503. dol_syslog("We regenerate alias page new name=".$filealias.", old name=".$fileoldalias);
  504. dol_delete_file($filetplold);
  505. // Create new file
  506. $objectpagenew = $objectpageold->createFromClone($user, $pageid, $objectpageold->pageurl, '', 0, $object->id, 1);
  507. //print $pageid.' = '.$objectpageold->pageurl.' -> '.$objectpagenew->id.' = '.$objectpagenew->pageurl.'<br>';
  508. if (is_object($objectpagenew) && $objectpagenew->pageurl)
  509. {
  510. $filealias=$pathofwebsitenew.'/'.$objectpagenew->pageurl.'.php';
  511. $filetplnew=$pathofwebsitenew.'/page'.$objectpagenew->id.'.tpl.php';
  512. // Save page alias
  513. $result=dolSavePageAlias($filealias, $object, $objectpagenew);
  514. if (! $result) setEventMessages('Failed to write file '.$filealias, null, 'errors');
  515. $result=dolSavePageContent($filetplnew, $object, $objectpagenew);
  516. if (! $result) setEventMessages('Failed to write file '.$filetplnew, null, 'errors');
  517. if ($pageid == $oldidforhome)
  518. {
  519. $newidforhome = $objectpagenew->id;
  520. }
  521. }
  522. else
  523. {
  524. setEventMessages($objectpageold->error, $objectpageold->errors, 'errors');
  525. $error++;
  526. }
  527. }
  528. }
  529. if (! $error)
  530. {
  531. // Restore id of home page
  532. $object->fk_default_home = $newidforhome;
  533. $res = $object->update($user);
  534. if (! $res > 0)
  535. {
  536. $error++;
  537. setEventMessages($objectpage->error, $objectpage->errors, 'errors');
  538. }
  539. if (! $error)
  540. {
  541. $filetpl=$pathofwebsitenew.'/page'.$newidforhome.'.tpl.php';
  542. $filewrapper=$pathofwebsitenew.'/wrapper.php';
  543. // Generate the index.php page to be the home page
  544. //-------------------------------------------------
  545. $result = dolSaveIndexPage($pathofwebsitenew, $fileindex, $filetpl, $filewrapper);
  546. }
  547. }
  548. unset($object->context['createfromclone']);
  549. // End
  550. if (!$error) {
  551. $this->db->commit();
  552. return $object;
  553. } else {
  554. $this->db->rollback();
  555. return - 1;
  556. }
  557. }
  558. /**
  559. * Return a link to the user card (with optionaly the picto)
  560. * Use this->id,this->lastname, this->firstname
  561. *
  562. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  563. * @param string $option On what the link point to
  564. * @param integer $notooltip 1=Disable tooltip
  565. * @param int $maxlen Max length of visible user name
  566. * @param string $morecss Add more css on link
  567. * @return string String with URL
  568. */
  569. function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
  570. {
  571. global $langs, $conf, $db;
  572. global $dolibarr_main_authentication, $dolibarr_main_demo;
  573. global $menumanager;
  574. $result = '';
  575. $companylink = '';
  576. $label = '<u>' . $langs->trans("WebSite") . '</u>';
  577. $label.= '<div width="100%">';
  578. $label.= '<b>' . $langs->trans('Nom') . ':</b> ' . $this->ref;
  579. $linkstart = '<a href="'.DOL_URL_ROOT.'/website/card.php?id='.$this->id.'"';
  580. $linkstart.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
  581. $linkstart.= '>';
  582. $linkend='</a>';
  583. $linkstart = $linkend = '';
  584. if ($withpicto)
  585. {
  586. $result.=($linkstart.img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?'':'class="classfortooltip"')).$linkend);
  587. if ($withpicto != 2) $result.=' ';
  588. }
  589. $result.= $linkstart . $this->ref . $linkend;
  590. return $result;
  591. }
  592. /**
  593. * Retourne le libelle du status d'un user (actif, inactif)
  594. *
  595. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  596. * @return string Label of status
  597. */
  598. function getLibStatut($mode=0)
  599. {
  600. return $this->LibStatut($this->status,$mode);
  601. }
  602. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  603. /**
  604. * Renvoi le libelle d'un status donne
  605. *
  606. * @param int $status Id status
  607. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  608. * @return string Label of status
  609. */
  610. function LibStatut($status,$mode=0)
  611. {
  612. // phpcs:enable
  613. global $langs;
  614. if ($mode == 0 || $mode == 1)
  615. {
  616. if ($status == 1) return $langs->trans('Enabled');
  617. if ($status == 0) return $langs->trans('Disabled');
  618. }
  619. elseif ($mode == 2)
  620. {
  621. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  622. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  623. }
  624. elseif ($mode == 3)
  625. {
  626. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
  627. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
  628. }
  629. elseif ($mode == 4)
  630. {
  631. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  632. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  633. }
  634. elseif ($mode == 5)
  635. {
  636. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  637. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  638. }
  639. }
  640. /**
  641. * Initialise object with example values
  642. * Id must be 0 if object instance is a specimen
  643. *
  644. * @return void
  645. */
  646. public function initAsSpecimen()
  647. {
  648. global $user;
  649. $this->id = 0;
  650. $this->entity = 1;
  651. $this->ref = 'myspecimenwebsite';
  652. $this->description = 'A specimen website';
  653. $this->status = '';
  654. $this->fk_default_home = null;
  655. $this->virtualhost = 'http://myvirtualhost';
  656. $this->fk_user_creat = $user->id;
  657. $this->fk_user_modif = $user->id;
  658. $this->date_creation = dol_now();
  659. $this->tms = dol_now();
  660. }
  661. /**
  662. * Generate a zip with all data of web site.
  663. *
  664. * @return string Path to file with zip
  665. */
  666. function exportWebSite()
  667. {
  668. global $conf, $mysoc;
  669. $website = $this;
  670. if (empty($website->id) || empty($website->ref))
  671. {
  672. setEventMessages("Website id or ref is not defined", null, 'errors');
  673. return '';
  674. }
  675. dol_syslog("Create temp dir ".$conf->website->dir_temp);
  676. dol_mkdir($conf->website->dir_temp);
  677. if (! is_writable($conf->website->dir_temp))
  678. {
  679. setEventMessages("Temporary dir ".$conf->website->dir_temp." is not writable", null, 'errors');
  680. return '';
  681. }
  682. $destdir = $conf->website->dir_temp.'/'.$website->ref;
  683. dol_syslog("Clear temp dir ".$destdir);
  684. $count=0; $countreallydeleted=0;
  685. $counttodelete = dol_delete_dir_recursive($destdir, $count, 1, 0, $countreallydeleted);
  686. if ($counttodelete != $countreallydeleted)
  687. {
  688. setEventMessages("Failed to clean temp directory ".$destdir, null, 'errors');
  689. return '';
  690. }
  691. $arrayreplacement=array();
  692. $srcdir = $conf->website->dir_output.'/'.$website->ref;
  693. $destdir = $conf->website->dir_temp.'/'.$website->ref.'/containers';
  694. dol_syslog("Copy content from ".$srcdir." into ".$destdir);
  695. dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
  696. $srcdir = DOL_DATA_ROOT.'/medias/image/'.$website->ref;
  697. $destdir = $conf->website->dir_temp.'/'.$website->ref.'/medias/image/websitekey';
  698. dol_syslog("Copy content from ".$srcdir." into ".$destdir);
  699. dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
  700. $srcdir = DOL_DATA_ROOT.'/medias/js/'.$website->ref;
  701. $destdir = $conf->website->dir_temp.'/'.$website->ref.'/medias/js/websitekey';
  702. dol_syslog("Copy content from ".$srcdir." into ".$destdir);
  703. dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
  704. // Build sql file
  705. dol_syslog("Create containers dir");
  706. dol_mkdir($conf->website->dir_temp.'/'.$website->ref.'/containers');
  707. $filesql = $conf->website->dir_temp.'/'.$website->ref.'/website_pages.sql';
  708. $fp = fopen($filesql,"w");
  709. if (empty($fp))
  710. {
  711. setEventMessages("Failed to create file ".$filesql, null, 'errors');
  712. return '';
  713. }
  714. $objectpages = new WebsitePage($this->db);
  715. $listofpages = $objectpages->fetchAll($website->id);
  716. // Assign ->newid and ->newfk_page
  717. $i=1;
  718. foreach($listofpages as $pageid => $objectpageold)
  719. {
  720. $objectpageold->newid=$i;
  721. $i++;
  722. }
  723. $i=1;
  724. foreach($listofpages as $pageid => $objectpageold)
  725. {
  726. // Search newid
  727. $newfk_page=0;
  728. foreach($listofpages as $pageid2 => $objectpageold2)
  729. {
  730. if ($pageid2 == $objectpageold->fk_page)
  731. {
  732. $newfk_page = $objectpageold2->newid;
  733. break;
  734. }
  735. }
  736. $objectpageold->newfk_page=$newfk_page;
  737. $i++;
  738. }
  739. foreach($listofpages as $pageid => $objectpageold)
  740. {
  741. $allaliases = $objectpageold->pageurl;
  742. $allaliases.= ($objectpageold->aliasalt ? ','.$objectpageold->aliasalt : '');
  743. $line = '-- Page ID '.$objectpageold->id.' -> '.$objectpageold->newid.'__+MAX_llx_website_page__ - Aliases '.$allaliases.' --;'; // newid start at 1, 2...
  744. $line.= "\n";
  745. fputs($fp, $line);
  746. // Warning: We must keep llx_ here. It is a generic SQL.
  747. $line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, aliasalt, title, description, image, keywords, status, date_creation, tms, lang, import_key, grabbed_from, type_container, htmlheader, content)';
  748. $line.= " VALUES(";
  749. $line.= $objectpageold->newid."__+MAX_llx_website_page__, ";
  750. $line.= ($objectpageold->newfk_page ? $this->db->escape($objectpageold->newfk_page)."__+MAX_llx_website_page__" : "null").", ";
  751. $line.= "__WEBSITE_ID__, ";
  752. $line.= "'".$this->db->escape($objectpageold->pageurl)."', ";
  753. $line.= "'".$this->db->escape($objectpageold->aliasalt)."', ";
  754. $line.= "'".$this->db->escape($objectpageold->title)."', ";
  755. $line.= "'".$this->db->escape($objectpageold->description)."', ";
  756. $line.= "'".$this->db->escape($objectpageold->image)."', ";
  757. $line.= "'".$this->db->escape($objectpageold->keywords)."', ";
  758. $line.= "'".$this->db->escape($objectpageold->status)."', ";
  759. $line.= "'".$this->db->idate($objectpageold->date_creation)."', ";
  760. $line.= "'".$this->db->idate($objectpageold->date_modification)."', ";
  761. $line.= "'".$this->db->escape($objectpageold->lang)."', ";
  762. $line.= ($objectpageold->import_key ? "'".$this->db->escape($objectpageold->import_key)."'" : "null").", ";
  763. $line.= "'".$this->db->escape($objectpageold->grabbed_from)."', ";
  764. $line.= "'".$this->db->escape($objectpageold->type_container)."', ";
  765. $stringtoexport = $objectpageold->htmlheader;
  766. $stringtoexport = str_replace(array("\r\n","\r","\n"), "__N__", $stringtoexport);
  767. $stringtoexport = str_replace('file=image/'.$website->ref.'/', "file=image/__WEBSITE_KEY__/", $stringtoexport);
  768. $stringtoexport = str_replace('file=js/'.$website->ref.'/', "file=js/__WEBSITE_KEY__/", $stringtoexport);
  769. $stringtoexport = str_replace('medias/image/'.$website->ref.'/', "medias/image/__WEBSITE_KEY__/", $stringtoexport);
  770. $stringtoexport = str_replace('medias/js/'.$website->ref.'/', "medias/js/__WEBSITE_KEY__/", $stringtoexport);
  771. $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_small, "file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__", $stringtoexport);
  772. $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_mini, "file=logos%2Fthumbs%2F__LOGO_MINI_KEY__", $stringtoexport);
  773. $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo, "file=logos%2Fthumbs%2F__LOGO_KEY__", $stringtoexport);
  774. $line.= "'".$this->db->escape(str_replace(array("\r\n","\r","\n"), "__N__", $stringtoexport))."', "; // Replace \r \n to have record on 1 line
  775. $stringtoexport = $objectpageold->content;
  776. $stringtoexport = str_replace(array("\r\n","\r","\n"), "__N__", $stringtoexport);
  777. $stringtoexport = str_replace('file=image/'.$website->ref.'/', "file=image/__WEBSITE_KEY__/", $stringtoexport);
  778. $stringtoexport = str_replace('file=js/'.$website->ref.'/', "file=js/__WEBSITE_KEY__/", $stringtoexport);
  779. $stringtoexport = str_replace('medias/image/'.$website->ref.'/', "medias/image/__WEBSITE_KEY__/", $stringtoexport);
  780. $stringtoexport = str_replace('medias/js/'.$website->ref.'/', "medias/js/__WEBSITE_KEY__/", $stringtoexport);
  781. $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_small, "file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__", $stringtoexport);
  782. $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo_mini, "file=logos%2Fthumbs%2F__LOGO_MINI_KEY__", $stringtoexport);
  783. $stringtoexport = str_replace('file=logos%2Fthumbs%2F'.$mysoc->logo, "file=logos%2Fthumbs%2F__LOGO_KEY__", $stringtoexport);
  784. $line.= "'".$this->db->escape($stringtoexport)."'"; // Replace \r \n to have record on 1 line
  785. $line.= ");";
  786. $line.= "\n";
  787. fputs($fp, $line);
  788. // Add line to update home page id during import
  789. //var_dump($this->fk_default_home.' - '.$objectpageold->id.' - '.$objectpageold->newid);exit;
  790. if ($this->fk_default_home > 0 && ($objectpageold->id == $this->fk_default_home) && ($objectpageold->newid > 0)) // This is the record with home page
  791. {
  792. $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__;";
  793. $line.= "\n";
  794. fputs($fp, $line);
  795. }
  796. }
  797. fclose($fp);
  798. if (! empty($conf->global->MAIN_UMASK))
  799. @chmod($filesql, octdec($conf->global->MAIN_UMASK));
  800. // Build zip file
  801. $filedir = $conf->website->dir_temp.'/'.$website->ref.'/.';
  802. $fileglob = $conf->website->dir_temp.'/'.$website->ref.'/website_'.$website->ref.'-*.zip';
  803. $filename = $conf->website->dir_temp.'/'.$website->ref.'/website_'.$website->ref.'-'.dol_print_date(dol_now(),'dayhourlog').'.zip';
  804. dol_delete_file($fileglob, 0);
  805. dol_compress_file($filedir, $filename, 'zip');
  806. return $filename;
  807. }
  808. /**
  809. * Open a zip with all data of web site and load it into database.
  810. *
  811. * @param string $pathtofile Path of zip file
  812. * @return int <0 if KO, Id of new website if OK
  813. */
  814. function importWebSite($pathtofile)
  815. {
  816. global $conf, $mysoc;
  817. $error = 0;
  818. $object = $this;
  819. if (empty($object->ref))
  820. {
  821. $this->error = 'Function importWebSite called on object not loaded (object->ref is empty)';
  822. return -1;
  823. }
  824. dol_delete_dir_recursive(dirname($pathtofile).'/'.$object->ref);
  825. dol_mkdir(dirname($pathtofile).'/'.$object->ref);
  826. $filename = basename($pathtofile);
  827. if (! preg_match('/^website_(.*)-(.*)$/', $filename, $reg))
  828. {
  829. $this->errors[]='Bad format for filename '.$filename.'. Must be website_XXX-VERSION.';
  830. return -1;
  831. }
  832. $result = dol_uncompress($pathtofile, $conf->website->dir_temp.'/'.$object->ref);
  833. if (! empty($result['error']))
  834. {
  835. $this->errors[]='Failed to unzip file '.$pathtofile.'.';
  836. return -1;
  837. }
  838. dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/containers', $conf->website->dir_output.'/'.$object->ref, 0, 1); // Overwrite if exists
  839. // Now generate the master.inc.php page
  840. $filemaster=$conf->website->dir_output.'/'.$object->ref.'/master.inc.php';
  841. $result = dolSaveMasterFile($filemaster);
  842. if (! $result)
  843. {
  844. $this->errors[]='Failed to write file '.$filemaster;
  845. $error++;
  846. }
  847. 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
  848. 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
  849. $sqlfile = $conf->website->dir_temp.'/'.$object->ref.'/website_pages.sql';
  850. $arrayreplacement = array();
  851. $arrayreplacement['__WEBSITE_ID__'] = $object->id;
  852. $arrayreplacement['__WEBSITE_KEY__'] = $object->ref;
  853. $arrayreplacement['__N__'] = $this->db->escape("\n"); // Restore \n
  854. $arrayreplacement['__LOGO_SMALL_KEY__'] = $this->db->escape($mysoc->logo_small);
  855. $arrayreplacement['__LOGO_MINI_KEY__'] = $this->db->escape($mysoc->logo_mini);
  856. $arrayreplacement['__LOGO_KEY__'] = $this->db->escape($mysoc->logo);
  857. $result = dolReplaceInFile($sqlfile, $arrayreplacement);
  858. $this->db->begin();
  859. // Search the $maxrowid because we need it later
  860. $sqlgetrowid='SELECT MAX(rowid) as max from '.MAIN_DB_PREFIX.'website_page';
  861. $resql=$this->db->query($sqlgetrowid);
  862. if ($resql)
  863. {
  864. $obj=$this->db->fetch_object($resql);
  865. $maxrowid=$obj->max;
  866. }
  867. // Load sql record
  868. $runsql = run_sql($sqlfile, 1, '', 0, '', 'none', 0, 1); // The maxrowid of table is searched into this function two
  869. if ($runsql <= 0)
  870. {
  871. $this->errors[]='Failed to load sql file '.$sqlfile;
  872. $error++;
  873. }
  874. $objectpagestatic = new WebsitePage($this->db);
  875. // Make replacement of IDs
  876. $fp = fopen($sqlfile,"r");
  877. if ($fp)
  878. {
  879. while (! feof($fp))
  880. {
  881. // Warning fgets with second parameter that is null or 0 hang.
  882. $buf = fgets($fp, 65000);
  883. if (preg_match('/^-- Page ID (\d+)\s[^\s]+\s(\d+).*Aliases\s(.*)\s--;/i', $buf, $reg))
  884. {
  885. $oldid = $reg[1];
  886. $newid = ($reg[2] + $maxrowid);
  887. $aliasesarray = explode(',', $reg[3]);
  888. $objectpagestatic->fetch($newid);
  889. dol_syslog("Found ID ".$oldid." to replace with ID ".$newid." and shortcut aliases to create: ".$reg[3]);
  890. 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);
  891. // The move is not enough, so we regenerate page
  892. $filetpl=$conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php';
  893. dolSavePageContent($filetpl, $object, $objectpagestatic);
  894. // Regenerate alternative aliases pages
  895. foreach($aliasesarray as $aliasshortcuttocreate)
  896. {
  897. $filealias=$conf->website->dir_output.'/'.$object->ref.'/'.$aliasshortcuttocreate.'.php';
  898. dolSavePageAlias($filealias, $object, $objectpagestatic);
  899. }
  900. }
  901. }
  902. }
  903. // Regenerate index page to point to new index page
  904. $pathofwebsite = $conf->website->dir_output.'/'.$object->ref;
  905. dolSaveIndexPage($pathofwebsite, $pathofwebsite.'/index.php', $pathofwebsite.'/page'.$object->fk_default_home.'.tpl.php', $pathofwebsite.'/wrapper.php');
  906. if ($error)
  907. {
  908. $this->db->rollback();
  909. return -1;
  910. }
  911. else
  912. {
  913. $this->db->commit();
  914. return $object->id;
  915. }
  916. }
  917. /**
  918. * Component to select language inside a container (Full CSS Only)
  919. *
  920. * @param array|string $languagecodes 'auto' to show all languages available for page, or language codes array like array('en_US','fr_FR','de_DE','es_ES')
  921. * @param Translate $weblangs Language Object
  922. * @param string $morecss More CSS class on component
  923. * @param string $htmlname Suffix for HTML name
  924. * @return string HTML select component
  925. */
  926. public function componentSelectLang($languagecodes, $weblangs, $morecss='', $htmlname='')
  927. {
  928. global $websitepagefile, $website;
  929. if (! is_object($weblangs)) return 'ERROR componentSelectLang called with parameter $weblangs not defined';
  930. // Load tmppage if we have $websitepagefile defined
  931. $tmppage=new WebsitePage($this->db);
  932. $pageid = 0;
  933. if (! empty($websitepagefile))
  934. {
  935. $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
  936. if ($pageid > 0)
  937. {
  938. $tmppage->fetch($pageid);
  939. }
  940. }
  941. // Fill with existing translation, nothing if none
  942. if (! is_array($languagecodes) && $pageid > 0)
  943. {
  944. $languagecodes = array();
  945. $sql ="SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
  946. $sql.=" FROM ".MAIN_DB_PREFIX."website_page as wp";
  947. $sql.=" WHERE wp.fk_website = ".$website->id;
  948. $sql.=" AND (wp.fk_page = ".$pageid." OR wp.rowid = ".$pageid;
  949. if ($tmppage->fk_page > 0) $sql.=" OR wp.fk_page = ".$tmppage->fk_page." OR wp.rowid = ".$tmppage->fk_page;
  950. $sql.=")";
  951. $resql = $this->db->query($sql);
  952. if ($resql)
  953. {
  954. while ($obj = $this->db->fetch_object($resql))
  955. {
  956. $newlang = $obj->lang;
  957. if ($obj->rowid == $pageid) $newlang = $obj->lang;
  958. if (! in_array($newlang, $languagecodes)) $languagecodes[]=$newlang;
  959. }
  960. }
  961. }
  962. // Now $languagecodes is always an array
  963. $languagecodeselected= $weblangs->defaultlang; // Because we must init with a value, but real value is the lang of main parent container
  964. if (! empty($websitepagefile))
  965. {
  966. $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
  967. if ($pageid > 0)
  968. {
  969. $languagecodeselected=$tmppage->lang;
  970. if (! in_array($tmppage->lang, $languagecodes)) $languagecodes[]=$tmppage->lang; // We add language code of page into combo list
  971. }
  972. }
  973. $weblangs->load('languages');
  974. //var_dump($weblangs->defaultlang);
  975. $url = $_SERVER["REQUEST_URI"];
  976. $url = preg_replace('/(\?|&)l=([a-zA-Z_]*)/', '', $url); // We remove param l from url
  977. //$url = preg_replace('/(\?|&)lang=([a-zA-Z_]*)/', '', $url); // We remove param lang from url
  978. $url.= (preg_match('/\?/', $url) ? '&' : '?').'l=';
  979. $HEIGHTOPTION=40;
  980. $MAXHEIGHT = 4 * $HEIGHTOPTION;
  981. $nboflanguage = count($languagecodes);
  982. $out ='<!-- componentSelectLang'.$htmlname.' -->'."\n";
  983. $out.= '<style>';
  984. $out.= '.componentSelectLang'.$htmlname.':hover { height: '.min($MAXHEIGHT, ($HEIGHTOPTION * $nboflanguage)).'px; overflow-x: hidden; overflow-y: '.((($HEIGHTOPTION * $nboflanguage) > $MAXHEIGHT) ? ' scroll' : 'hidden').'; }'."\n";
  985. $out.= '.componentSelectLang'.$htmlname.' li { line-height: '.$HEIGHTOPTION.'px; }'."\n";
  986. $out.= '.componentSelectLang'.$htmlname.' {
  987. display: inline-block;
  988. padding: 0;
  989. height: '.$HEIGHTOPTION.'px;
  990. overflow: hidden;
  991. transition: all .3s ease;
  992. margin: 0 50px 0 0;
  993. vertical-align: top;
  994. }
  995. .componentSelectLang'.$htmlname.':hover, .componentSelectLang'.$htmlname.':hover a { background-color: #fff; color: #000 !important; }
  996. ul.componentSelectLang'.$htmlname.' { width: 150px; }
  997. ul.componentSelectLang'.$htmlname.':hover .fa { visibility: hidden; }
  998. .componentSelectLang'.$htmlname.' a { text-decoration: none; width: 100%; }
  999. .componentSelectLang'.$htmlname.' li { display: block; padding: 0px 20px; }
  1000. .componentSelectLang'.$htmlname.' li:hover { background-color: #EEE; }
  1001. ';
  1002. $out.= '</style>';
  1003. $out.= '<ul class="componentSelectLang'.$htmlname.($morecss?' '.$morecss:'').'">';
  1004. if ($languagecodeselected)
  1005. {
  1006. $shortcode = strtolower(substr($languagecodeselected, -2));
  1007. $label = $weblangs->trans("Language_".$languagecodeselected);
  1008. if ($shortcode == 'us') $label = preg_replace('/\s*\(.*\)/', '', $label);
  1009. $out.= '<a href="'.$url.$languagecodeselected.'"><li><img height="12px" src="medias/image/common/flags/'.$shortcode.'.png" style="margin-right: 5px;"/>'.$label;
  1010. $out.= '<span class="fa fa-caret-down" style="padding-left: 5px;" />';
  1011. $out.= '</li></a>';
  1012. }
  1013. $i=0;
  1014. foreach($languagecodes as $languagecode)
  1015. {
  1016. if ($languagecode == $languagecodeselected) continue; // Already output
  1017. $shortcode = strtolower(substr($languagecode, -2));
  1018. $label = $weblangs->trans("Language_".$languagecode);
  1019. if ($shortcode == 'us') $label = preg_replace('/\s*\(.*\)/', '', $label);
  1020. $out.= '<a href="'.$url.$languagecode.'"><li><img height="12px" src="medias/image/common/flags/'.$shortcode.'.png" style="margin-right: 5px;"/>'.$label;
  1021. if (empty($i) && empty($languagecodeselected)) $out.= '<span class="fa fa-caret-down" style="padding-left: 5px;" />';
  1022. $out.= '</li></a>';
  1023. $i++;
  1024. }
  1025. $out.= '</ul>';
  1026. return $out;
  1027. }
  1028. }