website.class.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. <?php
  2. /* Copyright (C) 2007-2012 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) ---Put here your own copyright and developer email---
  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. /**
  72. * @var mixed
  73. */
  74. public $tms = '';
  75. /**
  76. * @var integer
  77. */
  78. public $fk_default_home;
  79. /**
  80. * @var string
  81. */
  82. public $virtualhost;
  83. /**
  84. * Constructor
  85. *
  86. * @param DoliDb $db Database handler
  87. */
  88. public function __construct(DoliDB $db)
  89. {
  90. $this->db = $db;
  91. return 1;
  92. }
  93. /**
  94. * Create object into database
  95. *
  96. * @param User $user User that creates
  97. * @param bool $notrigger false=launch triggers after, true=disable triggers
  98. *
  99. * @return int <0 if KO, Id of created object if OK
  100. */
  101. public function create(User $user, $notrigger = false)
  102. {
  103. global $conf;
  104. dol_syslog(__METHOD__, LOG_DEBUG);
  105. $error = 0;
  106. $now=dol_now();
  107. // Clean parameters
  108. if (isset($this->entity)) {
  109. $this->entity = trim($this->entity);
  110. }
  111. if (isset($this->ref)) {
  112. $this->ref = trim($this->ref);
  113. }
  114. if (isset($this->description)) {
  115. $this->description = trim($this->description);
  116. }
  117. if (isset($this->status)) {
  118. $this->status = trim($this->status);
  119. }
  120. if (empty($this->date_creation)) $this->date_creation = $now;
  121. if (empty($this->date_modification)) $this->date_modification = $now;
  122. // Check parameters
  123. if (empty($this->entity)) { $this->entity = $conf->entity; }
  124. // Insert request
  125. $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
  126. $sql.= 'entity,';
  127. $sql.= 'ref,';
  128. $sql.= 'description,';
  129. $sql.= 'status,';
  130. $sql.= 'fk_default_home,';
  131. $sql.= 'virtualhost,';
  132. $sql.= 'fk_user_create,';
  133. $sql.= 'date_creation,';
  134. $sql.= 'tms';
  135. $sql .= ') VALUES (';
  136. $sql .= ' '.((empty($this->entity) && $this->entity != '0')?'NULL':$this->entity).',';
  137. $sql .= ' '.(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").',';
  138. $sql .= ' '.(! isset($this->description)?'NULL':"'".$this->db->escape($this->description)."'").',';
  139. $sql .= ' '.(! isset($this->status)?'NULL':$this->status).',';
  140. $sql .= ' '.(! isset($this->fk_default_home)?'NULL':$this->fk_default_home).',';
  141. $sql .= ' '.(! isset($this->virtualhost)?'NULL':"'".$this->db->escape($this->virtualhost)."'").",";
  142. $sql .= ' '.(! isset($this->fk_user_create)?$user->id:$this->fk_user_create).',';
  143. $sql .= ' '.(! isset($this->date_creation) || dol_strlen($this->date_creation)==0?'NULL':"'".$this->db->idate($this->date_creation)."'").",";
  144. $sql .= ' '.(! isset($this->date_modification) || dol_strlen($this->date_modification)==0?'NULL':"'".$this->db->idate($this->date_creation)."'");
  145. $sql .= ')';
  146. $this->db->begin();
  147. $resql = $this->db->query($sql);
  148. if (!$resql) {
  149. $error ++;
  150. $this->errors[] = 'Error ' . $this->db->lasterror();
  151. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  152. }
  153. if (!$error) {
  154. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  155. if (!$notrigger) {
  156. // Uncomment this and change MYOBJECT to your own tag if you
  157. // want this action to call a trigger.
  158. //// Call triggers
  159. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  160. //if ($result < 0) $error++;
  161. //// End call triggers
  162. }
  163. }
  164. // Commit or rollback
  165. if ($error) {
  166. $this->db->rollback();
  167. return - 1 * $error;
  168. } else {
  169. $this->db->commit();
  170. return $this->id;
  171. }
  172. }
  173. /**
  174. * Load object in memory from the database
  175. *
  176. * @param int $id Id object
  177. * @param string $ref Ref
  178. * @return int <0 if KO, 0 if not found, >0 if OK
  179. */
  180. public function fetch($id, $ref = null)
  181. {
  182. dol_syslog(__METHOD__, LOG_DEBUG);
  183. $sql = 'SELECT';
  184. $sql .= ' t.rowid,';
  185. $sql .= " t.entity,";
  186. $sql .= " t.ref,";
  187. $sql .= " t.description,";
  188. $sql .= " t.status,";
  189. $sql .= " t.fk_default_home,";
  190. $sql .= " t.virtualhost,";
  191. $sql .= " t.fk_user_create,";
  192. $sql .= " t.fk_user_modif,";
  193. $sql .= " t.date_creation,";
  194. $sql .= " t.tms as date_modification";
  195. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  196. $sql .= ' WHERE t.entity IN ('.getEntity('website').')';
  197. if (null !== $ref) {
  198. $sql .= " AND t.ref = '" . $this->db->escape($ref) . "'";
  199. } else {
  200. $sql .= ' AND t.rowid = ' . $id;
  201. }
  202. $resql = $this->db->query($sql);
  203. if ($resql) {
  204. $numrows = $this->db->num_rows($resql);
  205. if ($numrows) {
  206. $obj = $this->db->fetch_object($resql);
  207. $this->id = $obj->rowid;
  208. $this->entity = $obj->entity;
  209. $this->ref = $obj->ref;
  210. $this->description = $obj->description;
  211. $this->status = $obj->status;
  212. $this->fk_default_home = $obj->fk_default_home;
  213. $this->virtualhost = $obj->virtualhost;
  214. $this->fk_user_create = $obj->fk_user_create;
  215. $this->fk_user_modif = $obj->fk_user_modif;
  216. $this->date_creation = $this->db->jdate($obj->date_creation);
  217. $this->date_modification = $this->db->jdate($obj->date_modification);
  218. }
  219. $this->db->free($resql);
  220. if ($numrows > 0) {
  221. // Lines
  222. $this->fetchLines();
  223. }
  224. if ($numrows > 0) {
  225. return 1;
  226. } else {
  227. return 0;
  228. }
  229. } else {
  230. $this->errors[] = 'Error ' . $this->db->lasterror();
  231. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  232. return - 1;
  233. }
  234. }
  235. /**
  236. * Load object lines in memory from the database
  237. *
  238. * @return int <0 if KO, 0 if not found, >0 if OK
  239. */
  240. public function fetchLines()
  241. {
  242. $this->lines=array();
  243. // Load lines with object MyObjectLine
  244. return count($this->lines)?1:0;
  245. }
  246. /**
  247. * Load object in memory from the database
  248. *
  249. * @param string $sortorder Sort Order
  250. * @param string $sortfield Sort field
  251. * @param int $limit offset limit
  252. * @param int $offset offset limit
  253. * @param array $filter filter array
  254. * @param string $filtermode filter mode (AND or OR)
  255. *
  256. * @return int <0 if KO, >0 if OK
  257. */
  258. public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
  259. {
  260. dol_syslog(__METHOD__, LOG_DEBUG);
  261. $sql = 'SELECT';
  262. $sql .= ' t.rowid,';
  263. $sql .= " t.entity,";
  264. $sql .= " t.ref,";
  265. $sql .= " t.description,";
  266. $sql .= " t.status,";
  267. $sql .= " t.fk_default_home,";
  268. $sql .= " t.virtualhost,";
  269. $sql .= " t.fk_user_create,";
  270. $sql .= " t.fk_user_modif,";
  271. $sql .= " t.date_creation,";
  272. $sql .= " t.tms as date_modification";
  273. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
  274. $sql .= ' WHERE t.entity IN ('.getEntity('website').')';
  275. // Manage filter
  276. $sqlwhere = array();
  277. if (count($filter) > 0) {
  278. foreach ($filter as $key => $value) {
  279. $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  280. }
  281. }
  282. if (count($sqlwhere) > 0) {
  283. $sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere);
  284. }
  285. if (!empty($sortfield)) {
  286. $sql .= $this->db->order($sortfield,$sortorder);
  287. }
  288. if (!empty($limit)) {
  289. $sql .= ' ' . $this->db->plimit($limit, $offset);
  290. }
  291. $this->records = array();
  292. $resql = $this->db->query($sql);
  293. if ($resql) {
  294. $num = $this->db->num_rows($resql);
  295. while ($obj = $this->db->fetch_object($resql)) {
  296. $line = new self($this->db);
  297. $line->id = $obj->rowid;
  298. $line->entity = $obj->entity;
  299. $line->ref = $obj->ref;
  300. $line->description = $obj->description;
  301. $line->status = $obj->status;
  302. $line->fk_default_home = $obj->fk_default_home;
  303. $line->virtualhost = $obj->virtualhost;
  304. $this->fk_user_create = $obj->fk_user_create;
  305. $this->fk_user_modif = $obj->fk_user_modif;
  306. $line->date_creation = $this->db->jdate($obj->date_creation);
  307. $line->date_modification = $this->db->jdate($obj->date_modification);
  308. $this->records[$line->id] = $line;
  309. }
  310. $this->db->free($resql);
  311. return $num;
  312. } else {
  313. $this->errors[] = 'Error ' . $this->db->lasterror();
  314. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  315. return - 1;
  316. }
  317. }
  318. /**
  319. * Update object into database
  320. *
  321. * @param User $user User that modifies
  322. * @param bool $notrigger false=launch triggers after, true=disable triggers
  323. *
  324. * @return int <0 if KO, >0 if OK
  325. */
  326. public function update(User $user, $notrigger = false)
  327. {
  328. $error = 0;
  329. dol_syslog(__METHOD__, LOG_DEBUG);
  330. // Clean parameters
  331. if (isset($this->entity)) {
  332. $this->entity = trim($this->entity);
  333. }
  334. if (isset($this->ref)) {
  335. $this->ref = trim($this->ref);
  336. }
  337. if (isset($this->description)) {
  338. $this->description = trim($this->description);
  339. }
  340. if (isset($this->status)) {
  341. $this->status = trim($this->status);
  342. }
  343. // Check parameters
  344. // Put here code to add a control on parameters values
  345. // Update request
  346. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
  347. $sql .= ' entity = '.(isset($this->entity)?$this->entity:"null").',';
  348. $sql .= ' ref = '.(isset($this->ref)?"'".$this->db->escape($this->ref)."'":"null").',';
  349. $sql .= ' description = '.(isset($this->description)?"'".$this->db->escape($this->description)."'":"null").',';
  350. $sql .= ' status = '.(isset($this->status)?$this->status:"null").',';
  351. $sql .= ' fk_default_home = '.(($this->fk_default_home > 0)?$this->fk_default_home:"null").',';
  352. $sql .= ' virtualhost = '.(($this->virtualhost != '')?"'".$this->db->escape($this->virtualhost)."'":"null").',';
  353. $sql .= ' fk_user_modif = '.(! isset($this->fk_user_modif) ? $user->id : $this->fk_user_modif).',';
  354. $sql .= ' date_creation = '.(! isset($this->date_creation) || dol_strlen($this->date_creation) != 0 ? "'".$this->db->idate($this->date_creation)."'" : 'null');
  355. $sql .= ', tms = '.(dol_strlen($this->date_modification) != 0 ? "'".$this->db->idate($this->date_modification)."'" : "'".$this->db->idate(dol_now())."'");
  356. $sql .= ' WHERE rowid=' . $this->id;
  357. $this->db->begin();
  358. $resql = $this->db->query($sql);
  359. if (!$resql) {
  360. $error ++;
  361. $this->errors[] = 'Error ' . $this->db->lasterror();
  362. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  363. }
  364. if (!$error && !$notrigger) {
  365. // Uncomment this and change MYOBJECT to your own tag if you
  366. // want this action calls a trigger.
  367. //// Call triggers
  368. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  369. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  370. //// End call triggers
  371. }
  372. // Commit or rollback
  373. if ($error) {
  374. $this->db->rollback();
  375. return - 1 * $error;
  376. } else {
  377. $this->db->commit();
  378. return 1;
  379. }
  380. }
  381. /**
  382. * Delete object in database
  383. *
  384. * @param User $user User that deletes
  385. * @param bool $notrigger false=launch triggers after, true=disable triggers
  386. *
  387. * @return int <0 if KO, >0 if OK
  388. */
  389. public function delete(User $user, $notrigger = false)
  390. {
  391. dol_syslog(__METHOD__, LOG_DEBUG);
  392. $error = 0;
  393. $this->db->begin();
  394. if (!$error) {
  395. if (!$notrigger) {
  396. // Uncomment this and change MYOBJECT to your own tag if you
  397. // want this action calls a trigger.
  398. //// Call triggers
  399. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  400. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  401. //// End call triggers
  402. }
  403. }
  404. if (!$error) {
  405. $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
  406. $sql .= ' WHERE rowid=' . $this->id;
  407. $resql = $this->db->query($sql);
  408. if (!$resql) {
  409. $error ++;
  410. $this->errors[] = 'Error ' . $this->db->lasterror();
  411. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  412. }
  413. }
  414. if (! $error && ! empty($this->ref))
  415. {
  416. global $dolibarr_main_data_root;
  417. $pathofwebsite=$dolibarr_main_data_root.'/website/'.$this->ref;
  418. dol_delete_dir_recursive($pathofwebsite);
  419. }
  420. // Commit or rollback
  421. if ($error) {
  422. $this->db->rollback();
  423. return - 1 * $error;
  424. } else {
  425. $this->db->commit();
  426. return 1;
  427. }
  428. }
  429. /**
  430. * Load an object from its id and create a new one in database.
  431. * This copy website directories, regenerate all the pages + alias pages and recreate the medias link.
  432. *
  433. * @param User $user User making the clone
  434. * @param int $fromid Id of object to clone
  435. * @param string $newref New ref
  436. * @param string $newlang New language
  437. * @return mixed New object created, <0 if KO
  438. */
  439. public function createFromClone($user, $fromid, $newref, $newlang='')
  440. {
  441. global $hookmanager, $langs;
  442. global $dolibarr_main_data_root;
  443. $error=0;
  444. dol_syslog(__METHOD__, LOG_DEBUG);
  445. $object = new self($this->db);
  446. // Check no site with ref exists
  447. if ($object->fetch(0, $newref) > 0)
  448. {
  449. $this->error='NewRefIsAlreadyUsed';
  450. return -1;
  451. }
  452. $this->db->begin();
  453. // Load source object
  454. $object->fetch($fromid);
  455. $oldidforhome=$object->fk_default_home;
  456. $pathofwebsiteold=$dolibarr_main_data_root.'/website/'.$object->ref;
  457. $pathofwebsitenew=$dolibarr_main_data_root.'/website/'.$newref;
  458. dol_delete_dir_recursive($pathofwebsitenew);
  459. $fileindex=$pathofwebsitenew.'/index.php';
  460. // Reset some properties
  461. unset($object->id);
  462. unset($object->fk_user_creat);
  463. unset($object->import_key);
  464. // Clear fields
  465. $object->ref=$newref;
  466. $object->fk_default_home=0;
  467. $object->virtualhost='';
  468. // Create clone
  469. $object->context['createfromclone'] = 'createfromclone';
  470. $result = $object->create($user);
  471. if ($result < 0) {
  472. $error ++;
  473. $this->errors = $object->errors;
  474. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  475. }
  476. if (! $error)
  477. {
  478. dolCopyDir($pathofwebsiteold, $pathofwebsitenew, $conf->global->MAIN_UMASK, 0);
  479. // Check symlink to medias and restore it if ko
  480. $pathtomedias=DOL_DATA_ROOT.'/medias';
  481. $pathtomediasinwebsite=$pathofwebsitenew.'/medias';
  482. if (! is_link(dol_osencode($pathtomediasinwebsite)))
  483. {
  484. dol_syslog("Create symlink for ".$pathtomedias." into name ".$pathtomediasinwebsite);
  485. dol_mkdir(dirname($pathtomediasinwebsite)); // To be sure dir for website exists
  486. $result = symlink($pathtomedias, $pathtomediasinwebsite);
  487. }
  488. $newidforhome=0;
  489. // Duplicate pages
  490. $objectpages = new WebsitePage($this->db);
  491. $listofpages = $objectpages->fetchAll($fromid);
  492. foreach($listofpages as $pageid => $objectpageold)
  493. {
  494. // Delete old file
  495. $filetplold=$pathofwebsitenew.'/page'.$pageid.'.tpl.php';
  496. dol_syslog("We regenerate alias page new name=".$filealias.", old name=".$fileoldalias);
  497. dol_delete_file($filetplold);
  498. // Create new file
  499. $objectpagenew = $objectpageold->createFromClone($user, $pageid, $objectpageold->pageurl, '', 0, $object->id, 1);
  500. //print $pageid.' = '.$objectpageold->pageurl.' -> '.$objectpagenew->id.' = '.$objectpagenew->pageurl.'<br>';
  501. if (is_object($objectpagenew) && $objectpagenew->pageurl)
  502. {
  503. $filealias=$pathofwebsitenew.'/'.$objectpagenew->pageurl.'.php';
  504. $filetplnew=$pathofwebsitenew.'/page'.$objectpagenew->id.'.tpl.php';
  505. // Save page alias
  506. $result=dolSavePageAlias($filealias, $object, $objectpagenew);
  507. if (! $result) setEventMessages('Failed to write file '.$filealias, null, 'errors');
  508. $result=dolSavePageContent($filetplnew, $object, $objectpagenew);
  509. if (! $result) setEventMessages('Failed to write file '.$filetplnew, null, 'errors');
  510. if ($pageid == $oldidforhome)
  511. {
  512. $newidforhome = $objectpagenew->id;
  513. }
  514. }
  515. else
  516. {
  517. setEventMessages($objectpageold->error, $objectpageold->errors, 'errors');
  518. $error++;
  519. }
  520. }
  521. }
  522. if (! $error)
  523. {
  524. // Restore id of home page
  525. $object->fk_default_home = $newidforhome;
  526. $res = $object->update($user);
  527. if (! $res > 0)
  528. {
  529. $error++;
  530. setEventMessages($objectpage->error, $objectpage->errors, 'errors');
  531. }
  532. if (! $error)
  533. {
  534. $filetpl=$pathofwebsitenew.'/page'.$newidforhome.'.tpl.php';
  535. // Generate the index.php page to be the home page
  536. //-------------------------------------------------
  537. $result = dolSaveIndexPage($pathofwebsitenew, $fileindex, $filetpl);
  538. }
  539. }
  540. // End
  541. if (!$error) {
  542. $this->db->commit();
  543. return $object;
  544. } else {
  545. $this->db->rollback();
  546. return - 1;
  547. }
  548. }
  549. /**
  550. * Return a link to the user card (with optionaly the picto)
  551. * Use this->id,this->lastname, this->firstname
  552. *
  553. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  554. * @param string $option On what the link point to
  555. * @param integer $notooltip 1=Disable tooltip
  556. * @param int $maxlen Max length of visible user name
  557. * @param string $morecss Add more css on link
  558. * @return string String with URL
  559. */
  560. function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
  561. {
  562. global $langs, $conf, $db;
  563. global $dolibarr_main_authentication, $dolibarr_main_demo;
  564. global $menumanager;
  565. $result = '';
  566. $companylink = '';
  567. $label = '<u>' . $langs->trans("WebSite") . '</u>';
  568. $label.= '<div width="100%">';
  569. $label.= '<b>' . $langs->trans('Nom') . ':</b> ' . $this->ref;
  570. $linkstart = '<a href="'.DOL_URL_ROOT.'/website/card.php?id='.$this->id.'"';
  571. $linkstart.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
  572. $linkstart.= '>';
  573. $linkend='</a>';
  574. $linkstart = $linkend = '';
  575. if ($withpicto)
  576. {
  577. $result.=($linkstart.img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?'':'class="classfortooltip"')).$linkend);
  578. if ($withpicto != 2) $result.=' ';
  579. }
  580. $result.= $linkstart . $this->ref . $linkend;
  581. return $result;
  582. }
  583. /**
  584. * Retourne le libelle du status d'un user (actif, inactif)
  585. *
  586. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  587. * @return string Label of status
  588. */
  589. function getLibStatut($mode=0)
  590. {
  591. return $this->LibStatut($this->status,$mode);
  592. }
  593. /**
  594. * Renvoi le libelle d'un status donne
  595. *
  596. * @param int $status Id status
  597. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  598. * @return string Label of status
  599. */
  600. // phpcs:ignore PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  601. function LibStatut($status,$mode=0)
  602. {
  603. global $langs;
  604. if ($mode == 0)
  605. {
  606. $prefix='';
  607. if ($status == 1) return $langs->trans('Enabled');
  608. if ($status == 0) return $langs->trans('Disabled');
  609. }
  610. if ($mode == 1)
  611. {
  612. if ($status == 1) return $langs->trans('Enabled');
  613. if ($status == 0) return $langs->trans('Disabled');
  614. }
  615. if ($mode == 2)
  616. {
  617. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  618. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  619. }
  620. if ($mode == 3)
  621. {
  622. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
  623. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
  624. }
  625. if ($mode == 4)
  626. {
  627. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  628. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  629. }
  630. if ($mode == 5)
  631. {
  632. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  633. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  634. }
  635. }
  636. /**
  637. * Initialise object with example values
  638. * Id must be 0 if object instance is a specimen
  639. *
  640. * @return void
  641. */
  642. public function initAsSpecimen()
  643. {
  644. global $user;
  645. $this->id = 0;
  646. $this->entity = 1;
  647. $this->ref = 'myspecimenwebsite';
  648. $this->description = 'A specimen website';
  649. $this->status = '';
  650. $this->fk_default_home = null;
  651. $this->virtualhost = 'http://myvirtualhost';
  652. $this->fk_user_create = $user->id;
  653. $this->fk_user_modif = $user->id;
  654. $this->date_creation = dol_now();
  655. $this->tms = dol_now();
  656. }
  657. /**
  658. * Generate a zip with all data of web site.
  659. *
  660. * @return string Path to file with zip
  661. */
  662. function exportWebSite()
  663. {
  664. global $conf;
  665. $website = $this;
  666. if (empty($website->id) || empty($website->ref))
  667. {
  668. setEventMessages("Website id or ref is not defined", null, 'errors');
  669. return '';
  670. }
  671. dol_syslog("Create temp dir ".$conf->website->dir_temp);
  672. dol_mkdir($conf->website->dir_temp);
  673. if (! is_writable($conf->website->dir_temp))
  674. {
  675. setEventMessages("Temporary dir ".$conf->website->dir_temp." is not writable", null, 'errors');
  676. return '';
  677. }
  678. $destdir = $conf->website->dir_temp.'/'.$website->ref;
  679. dol_syslog("Clear temp dir ".$destdir);
  680. $count=0; $countreallydeleted=0;
  681. $counttodelete = dol_delete_dir_recursive($destdir, $count, 1, 0, $countreallydeleted);
  682. if ($counttodelete != $countreallydeleted)
  683. {
  684. setEventMessages("Failed to clean temp directory ".$destdir, null, 'errors');
  685. return '';
  686. }
  687. $arrayreplacement=array();
  688. $srcdir = $conf->website->dir_output.'/'.$website->ref;
  689. $destdir = $conf->website->dir_temp.'/'.$website->ref.'/containers';
  690. dol_syslog("Copy content from ".$srcdir." into ".$destdir);
  691. dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
  692. $srcdir = DOL_DATA_ROOT.'/medias/image/'.$website->ref;
  693. $destdir = $conf->website->dir_temp.'/'.$website->ref.'/medias/image/websitekey';
  694. dol_syslog("Copy content from ".$srcdir." into ".$destdir);
  695. dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
  696. $srcdir = DOL_DATA_ROOT.'/medias/js/'.$website->ref;
  697. $destdir = $conf->website->dir_temp.'/'.$website->ref.'/medias/js/websitekey';
  698. dol_syslog("Copy content from ".$srcdir." into ".$destdir);
  699. dolCopyDir($srcdir, $destdir, 0, 1, $arrayreplacement);
  700. // Build sql file
  701. dol_syslog("Create containers dir");
  702. dol_mkdir($conf->website->dir_temp.'/'.$website->ref.'/containers');
  703. $filesql = $conf->website->dir_temp.'/'.$website->ref.'/website_pages.sql';
  704. $fp = fopen($filesql,"w");
  705. if (empty($fp))
  706. {
  707. setEventMessages("Failed to create file ".$filesql, null, 'errors');
  708. return '';
  709. }
  710. $objectpages = new WebsitePage($this->db);
  711. $listofpages = $objectpages->fetchAll($website->id);
  712. // Assign ->newid and ->newfk_page
  713. $i=1;
  714. foreach($listofpages as $pageid => $objectpageold)
  715. {
  716. $objectpageold->newid=$i;
  717. $i++;
  718. }
  719. $i=1;
  720. foreach($listofpages as $pageid => $objectpageold)
  721. {
  722. // Search newid
  723. $newfk_page=0;
  724. foreach($listofpages as $pageid2 => $objectpageold2)
  725. {
  726. if ($pageid2 == $objectpageold->fk_page)
  727. {
  728. $newfk_page = $objectpageold2->newid;
  729. break;
  730. }
  731. }
  732. $objectpageold->newfk_page=$newfk_page;
  733. $i++;
  734. }
  735. foreach($listofpages as $pageid => $objectpageold)
  736. {
  737. $allaliases = $objectpageold->pageurl;
  738. $allaliases.= ($objectpageold->aliasalt ? ','.$objectpageold->aliasalt : '');
  739. $line = '-- Page ID '.$objectpageold->id.' -> '.$objectpageold->newid.'__+MAX_llx_website_page__ - Aliases '.$allaliases.' --;';
  740. $line.= "\n";
  741. fputs($fp, $line);
  742. // Warning: We must keep llx_ here. It is a generic SQL.
  743. $line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, aliasalt, title, description, keywords, status, date_creation, tms, lang, import_key, grabbed_from, type_container, htmlheader, content)';
  744. $line.= " VALUES(";
  745. $line.= $objectpageold->newid."__+MAX_llx_website_page__, ";
  746. $line.= ($objectpageold->newfk_page ? $this->db->escape($objectpageold->newfk_page)."__+MAX_llx_website_page__" : "null").", ";
  747. $line.= "__WEBSITE_ID__, ";
  748. $line.= "'".$this->db->escape($objectpageold->pageurl)."', ";
  749. $line.= "'".$this->db->escape($objectpageold->aliasalt)."', ";
  750. $line.= "'".$this->db->escape($objectpageold->title)."', ";
  751. $line.= "'".$this->db->escape($objectpageold->description)."', ";
  752. $line.= "'".$this->db->escape($objectpageold->keywords)."', ";
  753. $line.= "'".$this->db->escape($objectpageold->status)."', ";
  754. $line.= "'".$this->db->idate($objectpageold->date_creation)."', ";
  755. $line.= "'".$this->db->idate($objectpageold->date_modification)."', ";
  756. $line.= "'".$this->db->escape($objectpageold->lang)."', ";
  757. $line.= ($objectpageold->import_key ? "'".$this->db->escape($objectpageold->import_key)."'" : "null").", ";
  758. $line.= "'".$this->db->escape($objectpageold->grabbed_from)."', ";
  759. $line.= "'".$this->db->escape($objectpageold->type_container)."', ";
  760. $stringtoexport = $objectpageold->htmlheader;
  761. $stringtoexport = str_replace(array("\r\n","\r","\n"), "__N__", $stringtoexport);
  762. $stringtoexport = str_replace('file=image/'.$website->ref.'/', "file=image/__WEBSITE_KEY__/", $stringtoexport);
  763. $stringtoexport = str_replace('file=js/'.$website->ref.'/', "file=js/__WEBSITE_KEY__/", $stringtoexport);
  764. $stringtoexport = str_replace('medias/image/'.$website->ref.'/', "medias/image/__WEBSITE_KEY__/", $stringtoexport);
  765. $stringtoexport = str_replace('medias/js/'.$website->ref.'/', "medias/js/__WEBSITE_KEY__/", $stringtoexport);
  766. $line.= "'".$this->db->escape(str_replace(array("\r\n","\r","\n"), "__N__", $stringtoexport))."', "; // Replace \r \n to have record on 1 line
  767. $stringtoexport = $objectpageold->content;
  768. $stringtoexport = str_replace(array("\r\n","\r","\n"), "__N__", $stringtoexport);
  769. $stringtoexport = str_replace('file=image/'.$website->ref.'/', "file=image/__WEBSITE_KEY__/", $stringtoexport);
  770. $stringtoexport = str_replace('file=js/'.$website->ref.'/', "file=js/__WEBSITE_KEY__/", $stringtoexport);
  771. $stringtoexport = str_replace('medias/image/'.$website->ref.'/', "medias/image/__WEBSITE_KEY__/", $stringtoexport);
  772. $stringtoexport = str_replace('medias/js/'.$website->ref.'/', "medias/js/__WEBSITE_KEY__/", $stringtoexport);
  773. $line.= "'".$this->db->escape($stringtoexport)."'"; // Replace \r \n to have record on 1 line
  774. $line.= ");";
  775. $line.= "\n";
  776. fputs($fp, $line);
  777. }
  778. fclose($fp);
  779. if (! empty($conf->global->MAIN_UMASK))
  780. @chmod($filesql, octdec($conf->global->MAIN_UMASK));
  781. // Build zip file
  782. $filedir = $conf->website->dir_temp.'/'.$website->ref.'/.';
  783. $fileglob = $conf->website->dir_temp.'/'.$website->ref.'/website_'.$website->ref.'-*.zip';
  784. $filename = $conf->website->dir_temp.'/'.$website->ref.'/website_'.$website->ref.'-'.dol_print_date(dol_now(),'dayhourlog').'.zip';
  785. dol_delete_file($fileglob, 0);
  786. dol_compress_file($filedir, $filename, 'zip');
  787. return $filename;
  788. }
  789. /**
  790. * Open a zip with all data of web site and load it into database.
  791. *
  792. * @param string $pathtofile Path of zip file
  793. * @return int <0 if KO, Id of new website if OK
  794. */
  795. function importWebSite($pathtofile)
  796. {
  797. global $conf;
  798. $error = 0;
  799. $object = $this;
  800. if (empty($object->ref))
  801. {
  802. $this->error = 'Function importWebSite called on object not loaded (object->ref is empty)';
  803. return -1;
  804. }
  805. dol_delete_dir_recursive(dirname($pathtofile).'/'.$object->ref);
  806. dol_mkdir(dirname($pathtofile).'/'.$object->ref);
  807. $filename = basename($pathtofile);
  808. if (! preg_match('/^website_(.*)-(.*)$/', $filename, $reg))
  809. {
  810. $this->errors[]='Bad format for filename '.$filename.'. Must be website_XXX-VERSION.';
  811. return -1;
  812. }
  813. $result = dol_uncompress($pathtofile, $conf->website->dir_temp.'/'.$object->ref);
  814. if (! empty($result['error']))
  815. {
  816. $this->errors[]='Failed to unzip file '.$pathtofile.'.';
  817. return -1;
  818. }
  819. dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/containers', $conf->website->dir_output.'/'.$object->ref, 0, 1); // Overwrite if exists
  820. 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
  821. 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
  822. $sqlfile = $conf->website->dir_temp.'/'.$object->ref.'/website_pages.sql';
  823. $arrayreplacement = array();
  824. $arrayreplacement['__WEBSITE_ID__'] = $object->id;
  825. $arrayreplacement['__WEBSITE_KEY__'] = $object->ref;
  826. $arrayreplacement['__N__'] = $this->db->escape("\n"); // Restore \n
  827. $result = dolReplaceInFile($sqlfile, $arrayreplacement);
  828. $this->db->begin();
  829. $sqlgetrowid='SELECT MAX(rowid) as max from '.MAIN_DB_PREFIX.'website_page';
  830. $resql=$this->db->query($sqlgetrowid);
  831. if ($resql)
  832. {
  833. $obj=$this->db->fetch_object($resql);
  834. $maxrowid=$obj->max;
  835. }
  836. $runsql = run_sql($sqlfile, 1, '', 0, '', 'none', 0, 1);
  837. if ($runsql <= 0)
  838. {
  839. $this->errors[]='Failed to load sql file '.$sqlfile.'.';
  840. $error++;
  841. }
  842. $objectpagestatic = new WebsitePage($this->db);
  843. // Make replacement of IDs
  844. $fp = fopen($sqlfile,"r");
  845. if ($fp)
  846. {
  847. while (! feof($fp))
  848. {
  849. // Warning fgets with second parameter that is null or 0 hang.
  850. $buf = fgets($fp, 65000);
  851. if (preg_match('/^-- Page ID (\d+)\s[^\s]+\s(\d+).*Aliases\s(.*)\s--;/i', $buf, $reg))
  852. {
  853. $oldid = $reg[1];
  854. $newid = ($reg[2] + $maxrowid);
  855. $aliasesarray = explode(',', $reg[3]);
  856. dol_syslog("Found ID ".$oldid." to replace with ID ".$newid." and shortcut aliases to create: ".$reg[3]);
  857. 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);
  858. foreach($aliasesarray as $aliasshortcuttocreate)
  859. {
  860. $objectpagestatic->id = $newid;
  861. $filealias=$conf->website->dir_output.'/'.$object->ref.'/'.$aliasshortcuttocreate.'.php';
  862. dolSavePageAlias($filealias, $object, $objectpagestatic);
  863. }
  864. }
  865. }
  866. }
  867. if ($error)
  868. {
  869. $this->db->rollback();
  870. return -1;
  871. }
  872. else
  873. {
  874. $this->db->commit();
  875. return $object->id;
  876. }
  877. }
  878. }