html.formwebsite.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/html.formwebsite.class.php
  19. * \ingroup core
  20. * \brief File of class to manage component html for module website
  21. */
  22. /**
  23. * Class to manage component html for module website
  24. */
  25. class FormWebsite
  26. {
  27. private $db;
  28. /**
  29. * @var string Error code (or message)
  30. */
  31. public $error;
  32. /**
  33. * var int A number of lines
  34. */
  35. public $num;
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. */
  41. public function __construct($db)
  42. {
  43. $this->db = $db;
  44. }
  45. /**
  46. * Return HTML select list of websites
  47. *
  48. * @param string $selected Id modele pre-selectionne
  49. * @param string $htmlname Name of HTML select
  50. * @param int $useempty Show empty value or not
  51. * @return string Html component
  52. */
  53. public function selectWebsite($selected = '', $htmlname = 'exportmodelid', $useempty = 0)
  54. {
  55. $out = '';
  56. $sql = "SELECT rowid, ref";
  57. $sql .= " FROM ".$this->db->prefix()."website";
  58. $sql .= " WHERE 1 = 1";
  59. $sql .= " ORDER BY rowid";
  60. $result = $this->db->query($sql);
  61. if ($result) {
  62. $out .= '<select class="flat minwidth100" name="'.$htmlname.'" id="'.$htmlname.'">';
  63. if ($useempty) {
  64. $out .= '<option value="-1">&nbsp;</option>';
  65. }
  66. $num = $this->db->num_rows($result);
  67. $i = 0;
  68. while ($i < $num) {
  69. $obj = $this->db->fetch_object($result);
  70. if ($selected == $obj->rowid) {
  71. $out .= '<option value="'.$obj->rowid.'" selected>';
  72. } else {
  73. $out .= '<option value="'.$obj->rowid.'">';
  74. }
  75. $out .= $obj->ref;
  76. $out .= '</option>';
  77. $i++;
  78. }
  79. $out .= "</select>";
  80. } else {
  81. dol_print_error($this->db);
  82. }
  83. return $out;
  84. }
  85. /**
  86. * Return a HTML select list of type of containers from the dictionary
  87. *
  88. * @param string $htmlname Name of select zone
  89. * @param string $selected Selected value
  90. * @param int $useempty 1=Add an empty value in list, 2=Add an empty value in list only if there is more than 2 entries.
  91. * @param string $moreattrib More attributes on HTML select tag
  92. * @param int $addjscombo Add js combo
  93. * @param string $morecss More CSS
  94. * @return void
  95. */
  96. public function selectTypeOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
  97. {
  98. global $langs, $conf, $user;
  99. $langs->load("admin");
  100. $sql = "SELECT rowid, code, label, entity";
  101. $sql .= " FROM ".$this->db->prefix().'c_type_container';
  102. $sql .= " WHERE active = 1 AND entity IN (".getEntity('c_type_container').")";
  103. $sql .= " ORDER BY label";
  104. dol_syslog(get_class($this)."::selectTypeOfContainer", LOG_DEBUG);
  105. $result = $this->db->query($sql);
  106. if ($result) {
  107. $num = $this->db->num_rows($result);
  108. $i = 0;
  109. if ($num) {
  110. print '<select id="select'.$htmlname.'" class="flat selectTypeOfContainer'.($morecss ? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
  111. if ($useempty == 1 || ($useempty == 2 && $num > 1)) {
  112. print '<option value="-1">&nbsp;</option>';
  113. }
  114. while ($i < $num) {
  115. $obj = $this->db->fetch_object($result);
  116. if ($selected == $obj->rowid || $selected == $obj->code) {
  117. print '<option value="'.$obj->code.'" selected>';
  118. } else {
  119. print '<option value="'.$obj->code.'">';
  120. }
  121. print $obj->label;
  122. print '</option>';
  123. $i++;
  124. }
  125. print "</select>";
  126. if ($user->admin) {
  127. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  128. }
  129. if ($addjscombo) {
  130. print ajax_combobox('select'.$htmlname);
  131. }
  132. } else {
  133. print $langs->trans("NoTypeOfPagePleaseEditDictionary");
  134. }
  135. } else {
  136. dol_print_error($this->db);
  137. }
  138. }
  139. /**
  140. * Return a HTML select list of samples of containers content
  141. *
  142. * @param string $htmlname Name of select zone
  143. * @param string $selected Selected value
  144. * @param int $useempty 1=Add an empty value in list
  145. * @param string $moreattrib More attributes on HTML select tag
  146. * @param int $addjscombo Add js combo
  147. * @param string $morecss More css
  148. * @return string HTML select component with list of type of containers
  149. */
  150. public function selectSampleOfContainer($htmlname, $selected = '', $useempty = 0, $moreattrib = '', $addjscombo = 0, $morecss = 'minwidth200')
  151. {
  152. global $langs, $conf, $user;
  153. $langs->load("admin");
  154. $listofsamples = dol_dir_list(DOL_DOCUMENT_ROOT.'/website/samples', 'files', 0, '^page-sample-.*\.html$');
  155. $arrayofsamples = array();
  156. $arrayofsamples['empty'] = 'EmptyPage'; // Always this one first
  157. foreach ($listofsamples as $sample) {
  158. $reg = array();
  159. if (preg_match('/^page-sample-(.*)\.html$/', $sample['name'], $reg)) {
  160. $key = $reg[1];
  161. $labelkey = ucfirst($key);
  162. if ($key == 'empty') {
  163. $labelkey = 'EmptyPage';
  164. }
  165. $arrayofsamples[$key] = $labelkey;
  166. }
  167. }
  168. $out = '';
  169. $out .= '<select id="select'.$htmlname.'" class="selectSampleOfContainer'.($morecss? ' '.$morecss : '').'" name="'.$htmlname.'"'.($moreattrib ? ' '.$moreattrib : '').'>';
  170. if ($useempty == 1 || $useempty == 2) {
  171. $out .= '<option value="-1">&nbsp;</option>';
  172. }
  173. foreach ($arrayofsamples as $key => $val) {
  174. if ($selected == $key) {
  175. $out .= '<option value="'.$key.'" selected>';
  176. } else {
  177. $out .= '<option value="'.$key.'">';
  178. }
  179. $out .= $langs->trans($val);
  180. $out .= '</option>';
  181. }
  182. $out .= "</select>";
  183. if ($addjscombo) {
  184. $out .= ajax_combobox('select'.$htmlname);
  185. }
  186. return $out;
  187. }
  188. /**
  189. * Return a HTML select list of containers of a website.
  190. * Note: $website->lines must have been loaded.
  191. *
  192. * @param Website $website Object Website
  193. * @param string $htmlname Name of select zone
  194. * @param int $pageid Preselected container ID
  195. * @param int $showempty Show empty record
  196. * @param string $action Action on page that use this select list
  197. * @param string $morecss More CSS
  198. * @param array $excludeids Exclude some ID in list
  199. * @return string HTML select component with list of block containers
  200. */
  201. public function selectContainer($website, $htmlname = 'pageid', $pageid = 0, $showempty = 0, $action = '', $morecss = 'minwidth200', $excludeids = null)
  202. {
  203. global $conf, $langs;
  204. $this->num = 0;
  205. $atleastonepage = (is_array($website->lines) && count($website->lines) > 0);
  206. $out = '';
  207. if ($atleastonepage && $action != 'editsource') {
  208. $out .= '<select name="'.$htmlname.'" id="'.$htmlname.'" class="maxwidth300'.($morecss ? ' '.$morecss : '').'">';
  209. } else {
  210. $out .= '<select name="pageidbis" id="pageid" class="maxwidth300'.($morecss ? ' '.$morecss : '').'"'.($action == 'editsource' ? ' disabled="disabled"' : '').'>';
  211. }
  212. if ($showempty || !$atleastonepage) {
  213. $out .= '<option class="optiongrey" value="-1">'.(is_numeric($showempty) ? '&nbsp;' : $showempty).'</option>';
  214. }
  215. /*if (!empty($conf->use_javascript_ajax)) {
  216. $valueoption = '<span class="classlink">'.img_picto('', 'add', 'class="paddingrightonly"').$langs->trans("AddPage").'</span>';
  217. $out .= '<option value="-2" data-html="'.dol_escape_htmltag($valueoption).'">'.$valueoption.'</option>';
  218. }*/
  219. if ($atleastonepage) {
  220. if (empty($pageid) && $action != 'createcontainer') { // Page id is not defined, we try to take one
  221. $firstpageid = 0;
  222. $homepageid = 0;
  223. foreach ($website->lines as $key => $valpage) {
  224. if (empty($firstpageid)) {
  225. $firstpageid = $valpage->id;
  226. }
  227. if ($website->fk_default_home && $key == $website->fk_default_home) {
  228. $homepageid = $valpage->id;
  229. }
  230. }
  231. $pageid = $homepageid ? $homepageid : $firstpageid; // We choose home page and if not defined yet, we take first page
  232. }
  233. foreach ($website->lines as $key => $valpage) {
  234. if (is_array($excludeids) && count($excludeids) && in_array($valpage->id, $excludeids)) {
  235. continue;
  236. }
  237. $valueforoption = '<span class="opacitymedium">['.$valpage->type_container.' '.sprintf("%03d", $valpage->id).']</span> ';
  238. $valueforoption .= $valpage->pageurl.' - '.$valpage->title;
  239. if ($website->otherlang) { // If there is alternative lang for this web site, we show the language code
  240. if ($valpage->lang) {
  241. $valueforoption .= ' <span class="opacitymedium">('.$valpage->lang.')</span>';
  242. }
  243. }
  244. if ($website->fk_default_home && $key == $website->fk_default_home) {
  245. //$valueforoption .= ' <span class="opacitymedium">('.$langs->trans("HomePage").')</span>';
  246. $valueforoption .= ' <span class="opacitymedium fa fa-home"></span>';
  247. }
  248. $out .= '<option value="'.$key.'"';
  249. if ($pageid > 0 && $pageid == $key) {
  250. $out .= ' selected'; // To preselect a value
  251. }
  252. $out .= ' data-html="'.dol_escape_htmltag($valueforoption).'"';
  253. $out .= '>';
  254. $out .= $valueforoption;
  255. $out .= '</option>';
  256. ++$this->num;
  257. }
  258. }
  259. $out .= '</select>';
  260. if ($atleastonepage && $action != 'editsource') {
  261. $out .= ajax_combobox($htmlname);
  262. } else {
  263. $out .= '<input type="hidden" name="'.$htmlname.'" value="'.$pageid.'">';
  264. $out .= ajax_combobox($htmlname);
  265. }
  266. return $out;
  267. }
  268. }