websitepage.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/website/class/websitepage.class.php
  22. * \ingroup website
  23. * \brief File for the CRUD class of websitepage (Create/Read/Update/Delete)
  24. */
  25. // Put here all includes required by your class file
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  27. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  28. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  29. /**
  30. * Class Websitepage
  31. */
  32. class WebsitePage extends CommonObject
  33. {
  34. /**
  35. * @var string Id to identify managed objects
  36. */
  37. public $element = 'websitepage';
  38. /**
  39. * @var string Name of table without prefix where object is stored
  40. */
  41. public $table_element = 'website_page';
  42. /**
  43. * @var string String with name of icon for websitepage. Must be the part after the 'object_' into object_myobject.png
  44. */
  45. public $picto = 'file-code';
  46. /**
  47. * @var int ID
  48. */
  49. public $fk_website;
  50. public $pageurl;
  51. public $aliasalt;
  52. public $type_container;
  53. /**
  54. * @var string title
  55. */
  56. public $title;
  57. /**
  58. * @var string description
  59. */
  60. public $description;
  61. /**
  62. * @var string image
  63. */
  64. public $image;
  65. /**
  66. * @var string keywords
  67. */
  68. public $keywords;
  69. /**
  70. * @var string language code ('en', 'fr', 'en-gb', ..)
  71. */
  72. public $lang;
  73. public $htmlheader;
  74. public $content;
  75. public $grabbed_from;
  76. /**
  77. * @var int Status
  78. */
  79. public $status;
  80. /**
  81. * @var integer|string date_creation
  82. */
  83. public $date_creation;
  84. /**
  85. * @var integer|string date_modification
  86. */
  87. public $date_modification;
  88. /**
  89. * @var string author_alias
  90. */
  91. public $author_alias;
  92. const STATUS_DRAFT = 0;
  93. const STATUS_VALIDATED = 1;
  94. // BEGIN MODULEBUILDER PROPERTIES
  95. /**
  96. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  97. */
  98. public $fields = array(
  99. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
  100. 'pageurl' =>array('type'=>'varchar(16)', 'label'=>'WEBSITE_PAGENAME', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Ref/alias of page'),
  101. 'aliasalt' =>array('type'=>'varchar(255)', 'label'=>'AliasAlt', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'index'=>0, 'position'=>11, 'searchall'=>0, 'comment'=>'Alias alternative of page'),
  102. 'type_container' =>array('type'=>'varchar(16)', 'label'=>'Type', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'index'=>0, 'position'=>12, 'comment'=>'Type of container'),
  103. 'title' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1, 'help'=>'UseTextBetween5And70Chars'),
  104. 'description' =>array('type'=>'varchar(255)', 'label'=>'Description', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1),
  105. 'image' =>array('type'=>'varchar(255)', 'label'=>'Image', 'enabled'=>1, 'visible'=>1, 'position'=>32, 'searchall'=>0, 'help'=>'Relative path of media. Used if Type is "blogpost"'),
  106. 'keywords' =>array('type'=>'varchar(255)', 'label'=>'Keywords', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'searchall'=>0),
  107. 'lang' =>array('type'=>'varchar(6)', 'label'=>'Lang', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'searchall'=>0),
  108. //'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>true, 'position'=>1000),
  109. 'fk_website' =>array('type'=>'integer', 'label'=>'WebsiteId', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>40, 'searchall'=>0, 'foreignkey'=>'websitepage.rowid'),
  110. 'fk_page' =>array('type'=>'integer', 'label'=>'ParentPageId', 'enabled'=>1, 'visible'=>1, 'notnull'=>-1, 'position'=>45, 'searchall'=>0, 'foreignkey'=>'website.rowid'),
  111. 'htmlheader' =>array('type'=>'text', 'label'=>'HtmlHeader', 'enabled'=>1, 'visible'=>0, 'position'=>50, 'searchall'=>0),
  112. 'content' =>array('type'=>'mediumtext', 'label'=>'Content', 'enabled'=>1, 'visible'=>0, 'position'=>51, 'searchall'=>0),
  113. 'grabbed_from' =>array('type'=>'varchar(255)', 'label'=>'GrabbedFrom', 'enabled'=>1, 'visible'=>1, 'index'=>1, 'position'=>400, 'comment'=>'URL page content was grabbed from'),
  114. 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
  115. 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>501),
  116. //'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>502),
  117. 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'position'=>510),
  118. 'author_alias' =>array('type'=>'varchar(64)', 'label'=>'AuthorAlias', 'enabled'=>1, 'visible'=>-1, 'index'=>0, 'position'=>511, 'comment'=>'Author alias'),
  119. 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
  120. //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
  121. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'index'=>1, 'position'=>1000, 'notnull'=>-1),
  122. );
  123. // END MODULEBUILDER PROPERTIES
  124. /**
  125. * Constructor
  126. *
  127. * @param DoliDb $db Database handler
  128. */
  129. public function __construct(DoliDB $db)
  130. {
  131. $this->db = $db;
  132. }
  133. /**
  134. * Create object into database
  135. *
  136. * @param User $user User that creates
  137. * @param bool $notrigger false=launch triggers after, true=disable triggers
  138. * @return int <0 if KO, Id of created object if OK
  139. */
  140. public function create(User $user, $notrigger = false)
  141. {
  142. $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
  143. $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
  144. if ($this->aliasalt) $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
  145. // Remove spaces and be sure we have main language only
  146. $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
  147. return $this->createCommon($user, $notrigger);
  148. }
  149. /**
  150. * Load object in memory from the database
  151. *
  152. * @param int $id Id object.
  153. * - If this is 0, the value into $page will be used. If not found or $page not defined, the default page of website_id will be used or the first page found if not set.
  154. * - If value is < 0, we must exclude this ID.
  155. * @param string $website_id Web site id (page name must also be filled if this parameter is used)
  156. * @param string $page Page name (website id must also be filled if this parameter is used). Exemple 'myaliaspage' or 'fr/myaliaspage'
  157. * @param string $aliasalt Alternative alias to search page (slow)
  158. *
  159. * @return int <0 if KO, 0 if not found, >0 if OK
  160. */
  161. public function fetch($id, $website_id = null, $page = null, $aliasalt = null)
  162. {
  163. dol_syslog(__METHOD__, LOG_DEBUG);
  164. $sql = 'SELECT';
  165. $sql .= ' t.rowid,';
  166. $sql .= " t.fk_website,";
  167. $sql .= ' t.type_container,';
  168. $sql .= " t.pageurl,";
  169. $sql .= " t.aliasalt,";
  170. $sql .= " t.title,";
  171. $sql .= " t.description,";
  172. $sql .= " t.image,";
  173. $sql .= " t.keywords,";
  174. $sql .= " t.htmlheader,";
  175. $sql .= " t.content,";
  176. $sql .= " t.lang,";
  177. $sql .= " t.fk_page,";
  178. $sql .= " t.status,";
  179. $sql .= " t.grabbed_from,";
  180. $sql .= " t.date_creation,";
  181. $sql .= " t.tms as date_modification,";
  182. $sql .= " t.fk_user_creat,";
  183. $sql .= " t.author_alias,";
  184. $sql .= " t.fk_user_modif,";
  185. $sql .= " t.import_key";
  186. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  187. //$sql .= ' WHERE entity IN ('.getEntity('website').')'; // entity is on website level
  188. $sql .= ' WHERE 1 = 1';
  189. if ($id > 0)
  190. {
  191. $sql .= ' AND t.rowid = '.$id;
  192. }
  193. else
  194. {
  195. if ($id < 0) $sql .= ' AND t.rowid <> '.abs($id);
  196. if (null !== $website_id) {
  197. $sql .= " AND t.fk_website = '".$this->db->escape($website_id)."'";
  198. if ($page) {
  199. $pagetouse = $page;
  200. $langtouse = '';
  201. $tmppage = explode('/', $page);
  202. if (! empty($tmppage[1])) {
  203. $pagetouse = $tmppage[1];
  204. if (strlen($tmppage[0])) $langtouse = $tmppage[0];
  205. }
  206. $sql .= " AND t.pageurl = '".$this->db->escape($pagetouse)."'";
  207. if ($langtouse) $sql .= " AND t.lang = '".$this->db->escape($langtouse)."'";
  208. }
  209. if ($aliasalt) $sql .= " AND (t.aliasalt LIKE '%,".$this->db->escape($aliasalt).",%' OR t.aliasalt LIKE '%, ".$this->db->escape($aliasalt).",%')";
  210. }
  211. }
  212. $sql .= $this->db->plimit(1);
  213. $resql = $this->db->query($sql);
  214. if ($resql) {
  215. $numrows = $this->db->num_rows($resql);
  216. if ($numrows) {
  217. $obj = $this->db->fetch_object($resql);
  218. $this->id = $obj->rowid;
  219. $this->fk_website = $obj->fk_website;
  220. $this->type_container = $obj->type_container;
  221. $this->pageurl = $obj->pageurl;
  222. $this->ref = $obj->pageurl;
  223. $this->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
  224. $this->title = $obj->title;
  225. $this->description = $obj->description;
  226. $this->image = $obj->image;
  227. $this->keywords = $obj->keywords;
  228. $this->htmlheader = $obj->htmlheader;
  229. $this->content = $obj->content;
  230. $this->lang = $obj->lang;
  231. $this->fk_page = $obj->fk_page;
  232. $this->status = $obj->status;
  233. $this->grabbed_from = $obj->grabbed_from;
  234. $this->date_creation = $this->db->jdate($obj->date_creation);
  235. $this->date_modification = $this->db->jdate($obj->date_modification);
  236. $this->fk_user_creat = $obj->fk_user_creat;
  237. $this->author_alias = $obj->author_alias;
  238. $this->fk_user_modif = $obj->fk_user_modif;
  239. $this->import_key = $obj->import_key;
  240. }
  241. $this->db->free($resql);
  242. if ($numrows) {
  243. return 1;
  244. } else {
  245. return 0;
  246. }
  247. } else {
  248. $this->errors[] = 'Error '.$this->db->lasterror();
  249. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  250. return -1;
  251. }
  252. }
  253. /**
  254. * Return array of all web site pages.
  255. *
  256. * @param string $websiteid Web site
  257. * @param string $sortorder Sort Order
  258. * @param string $sortfield Sort field
  259. * @param int $limit limit
  260. * @param int $offset Offset
  261. * @param array $filter Filter array
  262. * @param string $filtermode Filter mode (AND or OR)
  263. * @return array|int int <0 if KO, array of pages if OK
  264. */
  265. public function fetchAll($websiteid, $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  266. {
  267. dol_syslog(__METHOD__, LOG_DEBUG);
  268. $records = array();
  269. $sql = 'SELECT';
  270. $sql .= ' t.rowid,';
  271. $sql .= " t.fk_website,";
  272. $sql .= " t.type_container,";
  273. $sql .= " t.pageurl,";
  274. $sql .= " t.aliasalt,";
  275. $sql .= " t.title,";
  276. $sql .= " t.description,";
  277. $sql .= " t.image,";
  278. $sql .= " t.keywords,";
  279. $sql .= " t.htmlheader,";
  280. $sql .= " t.content,";
  281. $sql .= " t.lang,";
  282. $sql .= " t.fk_page,";
  283. $sql .= " t.status,";
  284. $sql .= " t.grabbed_from,";
  285. $sql .= " t.date_creation,";
  286. $sql .= " t.tms as date_modification,";
  287. $sql .= " t.fk_user_creat,";
  288. $sql .= " t.author_alias,";
  289. $sql .= " t.fk_user_modif,";
  290. $sql .= " t.import_key";
  291. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  292. $sql .= ' WHERE t.fk_website = '.$websiteid;
  293. // Manage filter
  294. $sqlwhere = array();
  295. if (count($filter) > 0) {
  296. foreach ($filter as $key => $value) {
  297. if ($key == 't.rowid' || $key == 't.fk_website') {
  298. $sqlwhere[] = $key.'='.$value;
  299. } elseif ($key == 'lang' || $key == 't.lang') {
  300. $sqlwhere[] = $key." = '".$this->db->escape(substr($value, 0, 2))."'";
  301. } else {
  302. $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
  303. }
  304. }
  305. }
  306. if (count($sqlwhere) > 0) {
  307. $sql .= ' AND ('.implode(' '.$filtermode.' ', $sqlwhere).')';
  308. }
  309. if (!empty($sortfield)) {
  310. $sql .= $this->db->order($sortfield, $sortorder);
  311. }
  312. if (!empty($limit)) {
  313. $sql .= ' '.$this->db->plimit($limit, $offset);
  314. }
  315. $resql = $this->db->query($sql);
  316. if ($resql) {
  317. $num = $this->db->num_rows($resql);
  318. while ($obj = $this->db->fetch_object($resql))
  319. {
  320. $record = new self($this->db);
  321. $record->id = $obj->rowid;
  322. $record->fk_website = $obj->fk_website;
  323. $record->type_container = $obj->type_container;
  324. $record->pageurl = $obj->pageurl;
  325. $record->aliasalt = preg_replace('/,+$/', '', preg_replace('/^,+/', '', $obj->aliasalt));
  326. $record->title = $obj->title;
  327. $record->description = $obj->description;
  328. $record->image = $obj->image;
  329. $record->keywords = $obj->keywords;
  330. $record->htmlheader = $obj->htmlheader;
  331. $record->content = $obj->content;
  332. $record->lang = $obj->lang;
  333. $record->fk_page = $obj->fk_page;
  334. $record->status = $obj->status;
  335. $record->grabbed_from = $obj->grabbed_from;
  336. $record->date_creation = $this->db->jdate($obj->date_creation);
  337. $record->date_modification = $this->db->jdate($obj->date_modification);
  338. $record->fk_user_creat = $obj->fk_user_creat;
  339. $record->author_alias = $obj->author_alias;
  340. $record->fk_user_modif = $obj->fk_user_modif;
  341. $record->import_key = $obj->import_key;
  342. //var_dump($record->id);
  343. $records[$record->id] = $record;
  344. }
  345. $this->db->free($resql);
  346. return $records;
  347. } else {
  348. $this->error = 'Error '.$this->db->lasterror();
  349. $this->errors[] = $this->error;
  350. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  351. return -1;
  352. }
  353. }
  354. /**
  355. * Count objects in the database.
  356. *
  357. * @param string $websiteid Web site
  358. * @param array $filter Filter array
  359. * @param string $filtermode Filter mode (AND or OR)
  360. * @return int int <0 if KO, array of pages if OK
  361. */
  362. public function countAll($websiteid, array $filter = array(), $filtermode = 'AND')
  363. {
  364. dol_syslog(__METHOD__, LOG_DEBUG);
  365. $result = 0;
  366. $sql = 'SELECT COUNT(t.rowid) as nb';
  367. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  368. $sql .= ' WHERE t.fk_website = '.$websiteid;
  369. // Manage filter
  370. $sqlwhere = array();
  371. if (count($filter) > 0) {
  372. foreach ($filter as $key => $value) {
  373. if ($key == 't.rowid' || $key == 't.fk_website') {
  374. $sqlwhere[] = $key.'='.$value;
  375. } elseif ($key == 'lang' || $key == 't.lang') {
  376. $sqlwhere[] = $key." = '".$this->db->escape(substr($value, 0, 2))."'";
  377. } else {
  378. $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
  379. }
  380. }
  381. }
  382. if (count($sqlwhere) > 0) {
  383. $sql .= ' AND ('.implode(' '.$filtermode.' ', $sqlwhere).')';
  384. }
  385. $resql = $this->db->query($sql);
  386. if ($resql) {
  387. $obj = $this->db->fetch_object($resql);
  388. if ($obj) {
  389. $result = $obj->nb;
  390. }
  391. $this->db->free($resql);
  392. return $result;
  393. } else {
  394. $this->error = 'Error '.$this->db->lasterror();
  395. $this->errors[] = $this->error;
  396. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  397. return -1;
  398. }
  399. }
  400. /**
  401. * Update object into database
  402. *
  403. * @param User $user User that modifies
  404. * @param bool $notrigger false=launch triggers after, true=disable triggers
  405. * @return int <0 if KO, >0 if OK
  406. */
  407. public function update(User $user, $notrigger = false)
  408. {
  409. $this->description = dol_trunc($this->description, 255, 'right', 'utf-8', 1);
  410. $this->keywords = dol_trunc($this->keywords, 255, 'right', 'utf-8', 1);
  411. if ($this->aliasalt) $this->aliasalt = ','.preg_replace('/,+$/', '', preg_replace('/^,+/', '', $this->aliasalt)).','; // content in database must be ',xxx,...,yyy,'
  412. // Remove spaces and be sure we have main language only
  413. $this->lang = preg_replace('/[_-].*$/', '', trim($this->lang)); // en_US or en-US -> en
  414. if ($this->fk_page > 0) {
  415. if (empty($this->lang)) {
  416. $this->error = "ErrorLanguageMandatoryIfPageSetAsTranslationOfAnother";
  417. return -1;
  418. }
  419. $tmppage = new WebsitePage($this->db);
  420. $tmppage->fetch($this->fk_page);
  421. if ($tmppage->lang == $this->lang) {
  422. $this->error = "ErrorLanguageOfTranslatedPageIsSameThanThisPage";
  423. return -1;
  424. }
  425. }
  426. return $this->updateCommon($user, $notrigger);
  427. }
  428. /**
  429. * Delete object in database
  430. *
  431. * @param User $user User that deletes
  432. * @param bool $notrigger false=launch triggers after, true=disable triggers
  433. * @return int <0 if KO, >0 if OK
  434. */
  435. public function delete(User $user, $notrigger = false)
  436. {
  437. $result = $this->deleteCommon($user, $trigger);
  438. if ($result > 0)
  439. {
  440. $websiteobj = new Website($this->db);
  441. $result = $websiteobj->fetch($this->fk_website);
  442. if ($result > 0)
  443. {
  444. global $dolibarr_main_data_root;
  445. $pathofwebsite = $dolibarr_main_data_root.'/website/'.$websiteobj->ref;
  446. $filealias = $pathofwebsite.'/'.$this->pageurl.'.php';
  447. $filetpl = $pathofwebsite.'/page'.$this->id.'.tpl.php';
  448. dol_delete_file($filealias);
  449. dol_delete_file($filetpl);
  450. }
  451. }
  452. return $result;
  453. }
  454. /**
  455. * Load an object from its id and create a new one in database
  456. *
  457. * @param User $user User making the clone
  458. * @param int $fromid Id of object to clone
  459. * @param string $newref New ref/alias of page
  460. * @param string $newlang New language
  461. * @param int $istranslation 1=New page is a translation of the cloned page.
  462. * @param int $newwebsite 0=Same web site, >0=Id of new website
  463. * @param string $newtitle New title
  464. * @return mixed New object created, <0 if KO
  465. */
  466. public function createFromClone(User $user, $fromid, $newref, $newlang = '', $istranslation = 0, $newwebsite = 0, $newtitle = '')
  467. {
  468. global $hookmanager, $langs;
  469. $now = dol_now();
  470. $error = 0;
  471. dol_syslog(__METHOD__, LOG_DEBUG);
  472. $object = new self($this->db);
  473. // Clean parameters
  474. if (empty($newref) && !empty($newtitle)) {
  475. $newref = strtolower(dol_sanitizeFileName(preg_replace('/\s+/', '-', $newtitle), '-', 1));
  476. }
  477. // Check parameters
  478. if (empty($newref)) {
  479. $langs->load("errors");
  480. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_TITLE"));
  481. return -1;
  482. }
  483. $this->db->begin();
  484. // Load source object
  485. $object->fetch($fromid);
  486. // Reset object
  487. $object->id = 0;
  488. // Clear fields
  489. $object->ref = $newref;
  490. $object->pageurl = $newref;
  491. $object->aliasalt = '';
  492. $object->fk_user_creat = $user->id;
  493. $object->author_alias = '';
  494. $object->date_creation = $now;
  495. $object->title = ($newtitle == '1' ? $object->title : ($newtitle ? $newtitle : $object->title));
  496. if (!empty($newlang)) $object->lang = $newlang;
  497. if ($istranslation) $object->fk_page = $fromid;
  498. else $object->fk_page = 0;
  499. if (!empty($newwebsite)) $object->fk_website = $newwebsite;
  500. $object->import_key = '';
  501. // Create clone
  502. $object->context['createfromclone'] = 'createfromclone';
  503. $result = $object->create($user);
  504. if ($result < 0) {
  505. $error++;
  506. $this->error = $object->error;
  507. $this->errors = $object->errors;
  508. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  509. }
  510. unset($object->context['createfromclone']);
  511. // End
  512. if (!$error) {
  513. $this->db->commit();
  514. return $object;
  515. } else {
  516. $this->db->rollback();
  517. return -1;
  518. }
  519. }
  520. /**
  521. * Return a link to the user card (with optionaly the picto)
  522. * Use this->id,this->lastname, this->firstname
  523. *
  524. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  525. * @param string $option On what the link point to
  526. * @param integer $notooltip 1=Disable tooltip
  527. * @param int $maxlen Max length of visible user name
  528. * @param string $morecss Add more css on link
  529. * @return string String with URL
  530. */
  531. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
  532. {
  533. global $langs, $conf, $db;
  534. global $dolibarr_main_authentication, $dolibarr_main_demo;
  535. global $menumanager;
  536. $result = '';
  537. $label = '<u>'.$langs->trans("Page").'</u>';
  538. $label .= '<br>';
  539. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
  540. $label .= '<b>'.$langs->trans('ID').':</b> '.$this->id.'<br>';
  541. $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
  542. $label .= '<b>'.$langs->trans('Language').':</b> '.$this->lang;
  543. $url = DOL_URL_ROOT.'/website/index.php?websiteid='.$this->fk_website.'&pageid='.$this->id;
  544. $linkclose = '';
  545. if (empty($notooltip))
  546. {
  547. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  548. {
  549. $label = $langs->trans("ShowMyObject");
  550. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  551. }
  552. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  553. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  554. }
  555. else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  556. $linkstart = '<a href="'.$url.'"';
  557. $linkstart .= $linkclose.'>';
  558. $linkend = '</a>';
  559. //$linkstart = $linkend = '';
  560. $result .= $linkstart;
  561. if ($withpicto) $result .= img_picto(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  562. if ($withpicto != 2) $result .= $this->ref;
  563. $result .= $linkend;
  564. return $result;
  565. }
  566. /**
  567. * Retourne le libelle du status d'un user (actif, inactif)
  568. *
  569. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  570. * @return string Label of status
  571. */
  572. public function getLibStatut($mode = 0)
  573. {
  574. return $this->LibStatut($this->status, $mode);
  575. }
  576. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  577. /**
  578. * Renvoi le libelle d'un status donne
  579. *
  580. * @param int $status Id status
  581. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  582. * @return string Label of status
  583. */
  584. public function LibStatut($status, $mode = 0)
  585. {
  586. // phpcs:enable
  587. global $langs;
  588. if (empty($this->labelStatus) || empty($this->labelStatusShort))
  589. {
  590. global $langs;
  591. //$langs->load("mymodule");
  592. $this->labelStatus[self::STATUS_DRAFT] = $langs->trans('Disabled');
  593. $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans('Enabled');
  594. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans('Disabled');
  595. $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->trans('Enabled');
  596. }
  597. $statusType = 'status5';
  598. if ($status == self::STATUS_VALIDATED) $statusType = 'status4';
  599. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  600. }
  601. /**
  602. * Initialise object with example values
  603. * Id must be 0 if object instance is a specimen
  604. *
  605. * @return void
  606. */
  607. public function initAsSpecimen()
  608. {
  609. global $user;
  610. $this->id = 0;
  611. $now = dol_now();
  612. $this->fk_website = '';
  613. $this->type_container = 'page';
  614. $this->pageurl = 'specimen';
  615. $this->aliasalt = 'specimenalt';
  616. $this->title = 'My Page';
  617. $this->description = 'This is my page';
  618. $this->image = '';
  619. $this->keywords = 'keyword1, keyword2';
  620. $this->htmlheader = '';
  621. $this->content = '<html><body>This is a html content</body></html>';
  622. $this->status = '';
  623. $this->grabbed_from = '';
  624. $this->date_creation = $now - (24 * 30 * 3600);
  625. $this->date_modification = $now - (24 * 7 * 3600);
  626. $this->fk_user_creat = $user->id;
  627. $this->author_alias = 'mypublicpseudo';
  628. }
  629. }