DolibarrModules.class.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  6. * Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  8. * Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  9. * Copyright (C) 2018 Josep Lluís Amador <joseplluis@lliuretic.cat>
  10. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/core/modules/DolibarrModules.class.php
  27. * \brief File of parent class of module descriptor class files
  28. */
  29. /**
  30. * Class DolibarrModules
  31. *
  32. * Parent class for module descriptor class files
  33. */
  34. class DolibarrModules // Can not be abstract, because we need to instantiate it into unActivateModule to be able to disable a module whose files were removed.
  35. {
  36. /**
  37. * @var DoliDb Database handler
  38. */
  39. public $db;
  40. /**
  41. * @var int Module unique ID
  42. * @see https://wiki.dolibarr.org/index.php/List_of_modules_id
  43. */
  44. public $numero;
  45. /**
  46. * @var string Publisher name
  47. * @since 4.0.0
  48. */
  49. public $editor_name;
  50. /**
  51. * @var string URL of module at publisher site
  52. * @since 4.0.0
  53. */
  54. public $editor_url;
  55. /**
  56. * @var string Family
  57. * @see $familyinfo
  58. *
  59. * Native values: 'crm', 'financial', 'hr', 'projects', 'products', 'ecm', 'technic', 'other'.
  60. * Use familyinfo to declare a custom value.
  61. */
  62. public $family;
  63. /**
  64. * @var array Custom family informations
  65. * @see $family
  66. *
  67. * e.g.:
  68. * array(
  69. * 'myownfamily' => array(
  70. * 'position' => '001',
  71. * 'label' => $langs->trans("MyOwnFamily")
  72. * )
  73. * );
  74. */
  75. public $familyinfo;
  76. /**
  77. * @var string Module position on 2 digits
  78. */
  79. public $module_position = '50';
  80. /**
  81. * @var string Module name
  82. *
  83. * Only used if Module[ID]Name translation string is not found.
  84. *
  85. * You can use the following code to automatically derive it from your module's class name:
  86. * preg_replace('/^mod/i', '', get_class($this))
  87. */
  88. public $name;
  89. /**
  90. * @var string[] Paths to create when module is activated
  91. *
  92. * e.g.: array('/mymodule/temp')
  93. */
  94. public $dirs = array();
  95. /**
  96. * @var array Module boxes
  97. */
  98. public $boxes = array();
  99. /**
  100. * @var array Module constants
  101. */
  102. public $const = array();
  103. /**
  104. * @var array Module cron jobs entries
  105. */
  106. public $cronjobs = array();
  107. /**
  108. * @var array Module access rights
  109. */
  110. public $rights;
  111. /**
  112. * @var string Module access rights family
  113. */
  114. public $rights_class;
  115. /**
  116. * @var array|int Module menu entries (1 means the menu entries are not declared into module descriptor but are hardcoded into menu manager)
  117. */
  118. public $menu = array();
  119. /**
  120. * @var array Module parts
  121. * array(
  122. * // Set this to 1 if module has its own trigger directory (/mymodule/core/triggers)
  123. * 'triggers' => 0,
  124. * // Set this to 1 if module has its own login method directory (/mymodule/core/login)
  125. * 'login' => 0,
  126. * // Set this to 1 if module has its own substitution function file (/mymodule/core/substitutions)
  127. * 'substitutions' => 0,
  128. * // Set this to 1 if module has its own menus handler directory (/mymodule/core/menus)
  129. * 'menus' => 0,
  130. * // Set this to 1 if module has its own theme directory (/mymodule/theme)
  131. * 'theme' => 0,
  132. * // Set this to 1 if module overwrite template dir (/mymodule/core/tpl)
  133. * 'tpl' => 0,
  134. * // Set this to 1 if module has its own barcode directory (/mymodule/core/modules/barcode)
  135. * 'barcode' => 0,
  136. * // Set this to 1 if module has its own models directory (/mymodule/core/modules/xxx)
  137. * 'models' => 0,
  138. * // Set this to relative path of css file if module has its own css file
  139. * 'css' => '/mymodule/css/mymodule.css.php',
  140. * // Set this to relative path of js file if module must load a js on all pages
  141. * 'js' => '/mymodule/js/mymodule.js',
  142. * // Set here all hooks context managed by module
  143. * 'hooks' => array('hookcontext1','hookcontext2')
  144. * )
  145. */
  146. public $module_parts = array();
  147. /**
  148. * @var string Module documents ?
  149. * @deprecated Seems unused anywhere
  150. */
  151. public $docs;
  152. /**
  153. * @var string ?
  154. * @deprecated Seems unused anywhere
  155. */
  156. public $dbversion = "-";
  157. /**
  158. * @var string Error message
  159. */
  160. public $error;
  161. /**
  162. * @var string Module version
  163. * @see http://semver.org
  164. *
  165. * The following keywords can also be used:
  166. * 'development'
  167. * 'experimental'
  168. * 'dolibarr': only for core modules that share its version
  169. * 'dolibarr_deprecated': only for deprecated core modules
  170. */
  171. public $version;
  172. /**
  173. * Module last version
  174. * @var string $lastVersion
  175. */
  176. public $lastVersion = '';
  177. /**
  178. * true indicate this module need update
  179. * @var bool $needUpdate
  180. */
  181. public $needUpdate = false;
  182. /**
  183. * @var string Module description (short text)
  184. *
  185. * Only used if Module[ID]Desc translation string is not found.
  186. */
  187. public $description;
  188. /**
  189. * @var string Module description (long text)
  190. * @since 4.0.0
  191. *
  192. * HTML content supported.
  193. */
  194. public $descriptionlong;
  195. // For exports
  196. /**
  197. * @var string Module export code
  198. */
  199. public $export_code;
  200. /**
  201. * @var string Module export label
  202. */
  203. public $export_label;
  204. public $export_icon;
  205. public $export_permission;
  206. public $export_fields_array;
  207. public $export_TypeFields_array; // Array of key=>type where type can be 'Numeric', 'Date', 'Text', 'Boolean', 'Status', 'List:xxx:login:rowid'
  208. public $export_entities_array;
  209. public $export_help_array;
  210. public $export_special_array; // special or computed field
  211. public $export_dependencies_array;
  212. public $export_sql_start;
  213. public $export_sql_end;
  214. public $export_sql_order;
  215. // For import
  216. /**
  217. * @var string Module import code
  218. */
  219. public $import_code;
  220. /**
  221. * @var string Module import label
  222. */
  223. public $import_label;
  224. public $import_icon;
  225. public $import_entities_array;
  226. public $import_tables_array;
  227. public $import_fields_array;
  228. public $import_fieldshidden_array;
  229. public $import_convertvalue_array;
  230. public $import_regex_array;
  231. public $import_examplevalues_array;
  232. public $import_updatekeys_array;
  233. /**
  234. * @var string Module constant name
  235. */
  236. public $const_name;
  237. /**
  238. * @var bool Module can't be disabled
  239. */
  240. public $always_enabled;
  241. /**
  242. * @var bool Module is disabled
  243. */
  244. public $disabled;
  245. /**
  246. * @var int Module is enabled globally (Multicompany support)
  247. */
  248. public $core_enabled;
  249. /**
  250. * @var string Name of image file used for this module
  251. *
  252. * If file is in theme/yourtheme/img directory under name object_pictoname.png use 'pictoname'
  253. * If file is in module/img directory under name object_pictoname.png use 'pictoname@module'
  254. */
  255. public $picto;
  256. /**
  257. * @var string[]|string List of config pages (Old modules uses a string. New one must use an array)
  258. *
  259. * Name of php pages stored into module/admin directory, used to setup module.
  260. * e.g.: array("setup.php@mymodule")
  261. */
  262. public $config_page_url;
  263. /**
  264. * @var string[] List of module class names that must be enabled if this module is enabled. e.g.: array('modAnotherModule', 'FR'=>'modYetAnotherModule')
  265. * @see $requiredby
  266. */
  267. public $depends;
  268. /**
  269. * @var string[] List of module class names to disable if the module is disabled.
  270. * @see $depends
  271. */
  272. public $requiredby;
  273. /**
  274. * @var string[] List of module class names as string this module is in conflict with.
  275. * @see $depends
  276. */
  277. public $conflictwith;
  278. /**
  279. * @var string[] Module language files
  280. */
  281. public $langfiles;
  282. /**
  283. * @var array<string,string> Array of warnings to show when we activate the module
  284. *
  285. * array('always'='text') or array('FR'='text')
  286. */
  287. public $warnings_activation;
  288. /**
  289. * @var array<string,string> Array of warnings to show when we activate an external module
  290. *
  291. * array('always'='text') or array('FR'='text')
  292. */
  293. public $warnings_activation_ext;
  294. /**
  295. * @var array Minimum version of PHP required by module.
  296. * e.g.: PHP ≥ 7.0 = array(7, 0)
  297. */
  298. public $phpmin;
  299. /**
  300. * @var array Minimum version of Dolibarr required by module.
  301. * e.g.: Dolibarr ≥ 3.6 = array(3, 6)
  302. */
  303. public $need_dolibarr_version;
  304. /**
  305. * @var bool Whether to hide the module.
  306. */
  307. public $hidden = false;
  308. /**
  309. * @var string url to check for module update
  310. */
  311. public $url_last_version;
  312. /**
  313. * Constructor. Define names, constants, directories, boxes, permissions
  314. *
  315. * @param DoliDB $db Database handler
  316. */
  317. public function __construct($db)
  318. {
  319. $this->db = $db;
  320. }
  321. // We should but can't set this as abstract because this will make dolibarr hang
  322. // after migration due to old module not implementing. We must wait PHP is able to make
  323. // a try catch on Fatal error to manage this correctly.
  324. // We need constructor into function unActivateModule into admin.lib.php
  325. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  326. /**
  327. * Enables a module.
  328. * Inserts all informations into database.
  329. *
  330. * @param array $array_sql SQL requests to be executed when enabling module
  331. * @param string $options String with options when disabling module:
  332. * - 'noboxes' = Do all actions but do not insert boxes
  333. * - 'newboxdefonly' = Do all actions but for boxes, insert def of boxes only and not boxes activation
  334. * @return int 1 if OK, 0 if KO
  335. */
  336. protected function _init($array_sql, $options = '')
  337. {
  338. // phpcs:enable
  339. global $conf;
  340. $err = 0;
  341. $this->db->begin();
  342. // Insert activation module constant
  343. if (!$err) {
  344. $err += $this->_active();
  345. }
  346. // Insert new pages for tabs (into llx_const)
  347. if (!$err) {
  348. $err += $this->insert_tabs();
  349. }
  350. // Insert activation of module's parts
  351. if (!$err) {
  352. $err += $this->insert_module_parts();
  353. }
  354. // Insert constant defined by modules (into llx_const)
  355. if (!$err && !preg_match('/newboxdefonly/', $options)) {
  356. $err += $this->insert_const(); // Test on newboxdefonly to avoid to erase value during upgrade
  357. }
  358. // Insert boxes def (into llx_boxes_def) and boxes setup (into llx_boxes)
  359. if (!$err && !preg_match('/noboxes/', $options)) {
  360. $err += $this->insert_boxes($options);
  361. }
  362. // Insert cron job entries (entry in llx_cronjobs)
  363. if (!$err) {
  364. $err += $this->insert_cronjobs();
  365. }
  366. // Insert permission definitions of module into llx_rights_def. If user is admin, grant this permission to user.
  367. if (!$err) {
  368. $err += $this->insert_permissions(1, null, 1);
  369. }
  370. // Insert specific menus entries into database
  371. if (!$err) {
  372. $err += $this->insert_menus();
  373. }
  374. // Create module's directories
  375. if (!$err) {
  376. $err += $this->create_dirs();
  377. }
  378. // Execute addons requests
  379. $num = count($array_sql);
  380. for ($i = 0; $i < $num; $i++) {
  381. if (!$err) {
  382. $val = $array_sql[$i];
  383. $sql = $val;
  384. $ignoreerror = 0;
  385. if (is_array($val)) {
  386. $sql = $val['sql'];
  387. $ignoreerror = $val['ignoreerror'];
  388. }
  389. // Add current entity id
  390. $sql = str_replace('__ENTITY__', $conf->entity, $sql);
  391. dol_syslog(get_class($this)."::_init ignoreerror=".$ignoreerror, LOG_DEBUG);
  392. $result = $this->db->query($sql, $ignoreerror);
  393. if (!$result) {
  394. if (!$ignoreerror) {
  395. $this->error = $this->db->lasterror();
  396. $err++;
  397. } else {
  398. dol_syslog(get_class($this)."::_init Warning ".$this->db->lasterror(), LOG_WARNING);
  399. }
  400. }
  401. }
  402. }
  403. // Return code
  404. if (!$err) {
  405. $this->db->commit();
  406. return 1;
  407. } else {
  408. $this->db->rollback();
  409. return 0;
  410. }
  411. }
  412. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  413. /**
  414. * Disable function. Deletes the module constants and boxes from the database.
  415. *
  416. * @param string[] $array_sql SQL requests to be executed when module is disabled
  417. * @param string $options Options when disabling module:
  418. *
  419. * @return int 1 if OK, 0 if KO
  420. */
  421. protected function _remove($array_sql, $options = '')
  422. {
  423. // phpcs:enable
  424. $err = 0;
  425. $this->db->begin();
  426. // Remove activation module line (constant MAIN_MODULE_MYMODULE in llx_const)
  427. if (!$err) {
  428. $err += $this->_unactive();
  429. }
  430. // Remove activation of module's new tabs (MAIN_MODULE_MYMODULE_TABS_XXX in llx_const)
  431. if (!$err) {
  432. $err += $this->delete_tabs();
  433. }
  434. // Remove activation of module's parts (MAIN_MODULE_MYMODULE_XXX in llx_const)
  435. if (!$err) {
  436. $err += $this->delete_module_parts();
  437. }
  438. // Remove constants defined by modules
  439. if (!$err) {
  440. $err += $this->delete_const();
  441. }
  442. // Remove list of module's available boxes (entry in llx_boxes)
  443. if (!$err && !preg_match('/(newboxdefonly|noboxes)/', $options)) {
  444. $err += $this->delete_boxes(); // We don't have to delete if option ask to keep boxes safe or ask to add new box def only
  445. }
  446. // Remove list of module's cron job entries (entry in llx_cronjobs)
  447. if (!$err) {
  448. $err += $this->delete_cronjobs();
  449. }
  450. // Remove module's permissions from list of available permissions (entries in llx_rights_def)
  451. if (!$err) {
  452. $err += $this->delete_permissions();
  453. }
  454. // Remove module's menus (entries in llx_menu)
  455. if (!$err) {
  456. $err += $this->delete_menus();
  457. }
  458. // Remove module's directories
  459. if (!$err) {
  460. $err += $this->delete_dirs();
  461. }
  462. // Run complementary sql requests
  463. $num = count($array_sql);
  464. for ($i = 0; $i < $num; $i++) {
  465. if (!$err) {
  466. dol_syslog(get_class($this)."::_remove", LOG_DEBUG);
  467. $result = $this->db->query($array_sql[$i]);
  468. if (!$result) {
  469. $this->error = $this->db->error();
  470. $err++;
  471. }
  472. }
  473. }
  474. // Return code
  475. if (!$err) {
  476. $this->db->commit();
  477. return 1;
  478. } else {
  479. $this->db->rollback();
  480. return 0;
  481. }
  482. }
  483. /**
  484. * Gives the translated module name if translation exists in admin.lang or into language files of module.
  485. * Otherwise return the module key name.
  486. *
  487. * @return string Translated module name
  488. */
  489. public function getName()
  490. {
  491. global $langs;
  492. $langs->load("admin");
  493. if ($langs->transnoentitiesnoconv("Module".$this->numero."Name") != ("Module".$this->numero."Name")) {
  494. // If module name translation exists
  495. return $langs->transnoentitiesnoconv("Module".$this->numero."Name");
  496. } else {
  497. // If module name translation using it's unique id does not exist, we try to use its name to find translation
  498. if (is_array($this->langfiles)) {
  499. foreach ($this->langfiles as $val) {
  500. if ($val) {
  501. $langs->load($val);
  502. }
  503. }
  504. }
  505. if ($langs->trans("Module".$this->name."Name") != ("Module".$this->name."Name")) {
  506. // If module name translation exists
  507. return $langs->transnoentitiesnoconv("Module".$this->name."Name");
  508. }
  509. // Last chance with simple label
  510. return $langs->transnoentitiesnoconv($this->name);
  511. }
  512. }
  513. /**
  514. * Gives the translated module description if translation exists in admin.lang or the default module description
  515. *
  516. * @return string Translated module description
  517. */
  518. public function getDesc()
  519. {
  520. global $langs;
  521. $langs->load("admin");
  522. if ($langs->transnoentitiesnoconv("Module".$this->numero."Desc") != ("Module".$this->numero."Desc")) {
  523. // If module description translation exists
  524. return $langs->transnoentitiesnoconv("Module".$this->numero."Desc");
  525. } else {
  526. // If module description translation does not exist using its unique id, we can use its name to find translation
  527. if (is_array($this->langfiles)) {
  528. foreach ($this->langfiles as $val) {
  529. if ($val) {
  530. $langs->load($val);
  531. }
  532. }
  533. }
  534. if ($langs->transnoentitiesnoconv("Module".$this->name."Desc") != ("Module".$this->name."Desc")) {
  535. // If module name translation exists
  536. return $langs->trans("Module".$this->name."Desc");
  537. }
  538. // Last chance with simple label
  539. return $langs->trans($this->description);
  540. }
  541. }
  542. /**
  543. * Gives the long description of a module. First check README-la_LA.md then README.md
  544. * If no markdown files found, it returns translated value of the key ->descriptionlong.
  545. *
  546. * @return string Long description of a module from README.md of from property.
  547. */
  548. public function getDescLong()
  549. {
  550. global $langs;
  551. $langs->load("admin");
  552. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  553. include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  554. $content='';
  555. $pathoffile = $this->getDescLongReadmeFound();
  556. if ($pathoffile) { // Mostly for external modules
  557. $content = file_get_contents($pathoffile);
  558. if ((float) DOL_VERSION >= 6.0) {
  559. @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
  560. $content = dolMd2Html(
  561. $content,
  562. 'parsedown',
  563. array(
  564. 'doc/' => dol_buildpath(strtolower($this->name).'/doc/', 1),
  565. 'img/' => dol_buildpath(strtolower($this->name).'/img/', 1),
  566. 'images/' => dol_buildpath(strtolower($this->name).'/images/', 1),
  567. )
  568. );
  569. } else {
  570. $content = nl2br($content);
  571. }
  572. } else {
  573. // Mostly for internal modules
  574. if (!empty($this->descriptionlong)) {
  575. if (is_array($this->langfiles)) {
  576. foreach ($this->langfiles as $val) {
  577. if ($val) {
  578. $langs->load($val);
  579. }
  580. }
  581. }
  582. $content = $langs->transnoentitiesnoconv($this->descriptionlong);
  583. }
  584. }
  585. return $content;
  586. }
  587. /**
  588. * Return path of file if a README file was found.
  589. *
  590. * @return string Path of file if a README file was found.
  591. */
  592. public function getDescLongReadmeFound()
  593. {
  594. global $langs;
  595. $filefound = false;
  596. // Define path to file README.md.
  597. // First check README-la_LA.md then README-la.md then README.md
  598. $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$langs->defaultlang.'.md', 0);
  599. if (dol_is_file($pathoffile)) {
  600. $filefound = true;
  601. }
  602. if (!$filefound) {
  603. $tmp = explode('_', $langs->defaultlang);
  604. $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$tmp[0].'.md', 0);
  605. if (dol_is_file($pathoffile)) {
  606. $filefound = true;
  607. }
  608. }
  609. if (!$filefound) {
  610. $pathoffile = dol_buildpath(strtolower($this->name).'/README.md', 0);
  611. if (dol_is_file($pathoffile)) {
  612. $filefound = true;
  613. }
  614. }
  615. return ($filefound ? $pathoffile : '');
  616. }
  617. /**
  618. * Gives the changelog. First check ChangeLog-la_LA.md then ChangeLog.md
  619. *
  620. * @return string Content of ChangeLog
  621. */
  622. public function getChangeLog()
  623. {
  624. global $langs;
  625. $langs->load("admin");
  626. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  627. include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  628. $filefound = false;
  629. // Define path to file README.md.
  630. // First check ChangeLog-la_LA.md then ChangeLog.md
  631. $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog-'.$langs->defaultlang.'.md', 0);
  632. if (dol_is_file($pathoffile)) {
  633. $filefound = true;
  634. }
  635. if (!$filefound) {
  636. $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog.md', 0);
  637. if (dol_is_file($pathoffile)) {
  638. $filefound = true;
  639. }
  640. }
  641. if ($filefound) { // Mostly for external modules
  642. $content = file_get_contents($pathoffile);
  643. if ((float) DOL_VERSION >= 6.0) {
  644. @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
  645. $content = dolMd2Html($content, 'parsedown', array('doc/'=>dol_buildpath(strtolower($this->name).'/doc/', 1)));
  646. } else {
  647. $content = nl2br($content);
  648. }
  649. }
  650. return $content;
  651. }
  652. /**
  653. * Gives the publisher name
  654. *
  655. * @return string Publisher name
  656. */
  657. public function getPublisher()
  658. {
  659. return $this->editor_name;
  660. }
  661. /**
  662. * Gives the publisher url
  663. *
  664. * @return string Publisher url
  665. */
  666. public function getPublisherUrl()
  667. {
  668. return $this->editor_url;
  669. }
  670. /**
  671. * Gives module version (translated if param $translated is on)
  672. * For 'experimental' modules, gives 'experimental' translation
  673. * For 'dolibarr' modules, gives Dolibarr version
  674. *
  675. * @param int $translated 1=Special version keys are translated, 0=Special version keys are not translated
  676. * @return string Module version
  677. */
  678. public function getVersion($translated = 1)
  679. {
  680. global $langs;
  681. $langs->load("admin");
  682. $ret = '';
  683. $newversion = preg_replace('/_deprecated/', '', $this->version);
  684. if ($newversion == 'experimental') {
  685. $ret = ($translated ? $langs->transnoentitiesnoconv("VersionExperimental") : $newversion);
  686. } elseif ($newversion == 'development') {
  687. $ret = ($translated ? $langs->transnoentitiesnoconv("VersionDevelopment") : $newversion);
  688. } elseif ($newversion == 'dolibarr') {
  689. $ret = DOL_VERSION;
  690. } elseif ($newversion) {
  691. $ret = $newversion;
  692. } else {
  693. $ret = ($translated ? $langs->transnoentitiesnoconv("VersionUnknown") : 'unknown');
  694. }
  695. if (preg_match('/_deprecated/', $this->version)) {
  696. $ret .= ($translated ? ' ('.$langs->transnoentitiesnoconv("Deprecated").')' : $this->version);
  697. }
  698. return $ret;
  699. }
  700. /**
  701. * Gives the module position
  702. *
  703. * @return int Module position (an external module should never return a value lower than 100000. 1-100000 are reserved for core)
  704. */
  705. public function getModulePosition()
  706. {
  707. if (in_array($this->version, array('dolibarr', 'experimental', 'development'))) { // core module
  708. return $this->module_position;
  709. } else { // external module
  710. if ($this->module_position >= 100000) {
  711. return $this->module_position;
  712. } else {
  713. return $this->module_position + 100000;
  714. }
  715. }
  716. }
  717. /**
  718. * Tells if module is core or external.
  719. * 'dolibarr' and 'dolibarr_deprecated' is core
  720. * 'experimental' and 'development' is core
  721. *
  722. * @return string 'core', 'external' or 'unknown'
  723. */
  724. public function isCoreOrExternalModule()
  725. {
  726. if ($this->version == 'dolibarr' || $this->version == 'dolibarr_deprecated') {
  727. return 'core';
  728. }
  729. if (!empty($this->version) && !in_array($this->version, array('experimental', 'development'))) {
  730. return 'external';
  731. }
  732. if (!empty($this->editor_name) || !empty($this->editor_url)) {
  733. return 'external';
  734. }
  735. if ($this->numero >= 100000) {
  736. return 'external';
  737. }
  738. return 'unknown';
  739. }
  740. /**
  741. * Gives module related language files list
  742. *
  743. * @return string[] Language files list
  744. */
  745. public function getLangFilesArray()
  746. {
  747. return $this->langfiles;
  748. }
  749. /**
  750. * Gives translated label of an export dataset
  751. *
  752. * @param int $r Dataset index
  753. *
  754. * @return string Translated databaset label
  755. */
  756. public function getExportDatasetLabel($r)
  757. {
  758. global $langs;
  759. $langstring = "ExportDataset_".$this->export_code[$r];
  760. if ($langs->trans($langstring) == $langstring) {
  761. // Translation not found
  762. return $langs->trans($this->export_label[$r]);
  763. } else {
  764. // Translation found
  765. return $langs->trans($langstring);
  766. }
  767. }
  768. /**
  769. * Gives translated label of an import dataset
  770. *
  771. * @param int $r Dataset index
  772. *
  773. * @return string Translated dataset label
  774. */
  775. public function getImportDatasetLabel($r)
  776. {
  777. global $langs;
  778. $langstring = "ImportDataset_".$this->import_code[$r];
  779. //print "x".$langstring;
  780. if ($langs->trans($langstring) == $langstring) {
  781. // Translation not found
  782. return $langs->transnoentitiesnoconv($this->import_label[$r]);
  783. } else {
  784. // Translation found
  785. return $langs->transnoentitiesnoconv($langstring);
  786. }
  787. }
  788. /**
  789. * Gives the last date of activation
  790. *
  791. * @return int|string Date of last activation or '' if module was never activated
  792. */
  793. public function getLastActivationDate()
  794. {
  795. global $conf;
  796. $err = 0;
  797. $sql = "SELECT tms FROM ".MAIN_DB_PREFIX."const";
  798. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  799. $sql .= " AND entity IN (0, ".$conf->entity.")";
  800. dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
  801. $resql = $this->db->query($sql);
  802. if (!$resql) {
  803. $err++;
  804. } else {
  805. $obj = $this->db->fetch_object($resql);
  806. if ($obj) {
  807. return $this->db->jdate($obj->tms);
  808. }
  809. }
  810. return '';
  811. }
  812. /**
  813. * Gives the last author of activation
  814. *
  815. * @return array Array array('authorid'=>Id of last activation user, 'lastactivationdate'=>Date of last activation)
  816. */
  817. public function getLastActivationInfo()
  818. {
  819. global $conf;
  820. $err = 0;
  821. $sql = "SELECT tms, note FROM ".MAIN_DB_PREFIX."const";
  822. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  823. $sql .= " AND entity IN (0, ".$conf->entity.")";
  824. dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
  825. $resql = $this->db->query($sql);
  826. if (!$resql) {
  827. $err++;
  828. } else {
  829. $obj = $this->db->fetch_object($resql);
  830. if ($obj) {
  831. $tmp = array();
  832. if ($obj->note) {
  833. $tmp = json_decode($obj->note, true);
  834. }
  835. return array(
  836. 'authorid' => $tmp['authorid'],
  837. 'ip' => $tmp['ip'],
  838. 'lastactivationdate' => $this->db->jdate($obj->tms),
  839. 'lastactivationversion' => (!empty($tmp['lastactivationversion']) ? $tmp['lastactivationversion'] : 'unknown'),
  840. );
  841. }
  842. }
  843. return array();
  844. }
  845. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  846. /**
  847. * Insert constants for module activation
  848. *
  849. * @return int Error count (0 if OK)
  850. */
  851. protected function _active()
  852. {
  853. // phpcs:enable
  854. global $conf, $user;
  855. $err = 0;
  856. // Common module
  857. $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
  858. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  859. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  860. $sql .= " AND entity IN (0, ".$entity.")";
  861. dol_syslog(get_class($this)."::_active delete activation constant", LOG_DEBUG);
  862. $resql = $this->db->query($sql);
  863. if (!$resql) {
  864. $err++;
  865. }
  866. $note = json_encode(
  867. array(
  868. 'authorid' => (is_object($user) ? $user->id : 0),
  869. 'ip' => (empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']),
  870. 'lastactivationversion' => $this->version,
  871. )
  872. );
  873. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, value, visible, entity, note) VALUES";
  874. $sql .= " (".$this->db->encrypt($this->const_name);
  875. $sql .= ", ".$this->db->encrypt('1');
  876. $sql .= ", 0, ".((int) $entity);
  877. $sql .= ", '".$this->db->escape($note)."')";
  878. dol_syslog(get_class($this)."::_active insert activation constant", LOG_DEBUG);
  879. $resql = $this->db->query($sql);
  880. if (!$resql) {
  881. $err++;
  882. }
  883. return $err;
  884. }
  885. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  886. /**
  887. * Module deactivation
  888. *
  889. * @return int Error count (0 if OK)
  890. */
  891. protected function _unactive()
  892. {
  893. // phpcs:enable
  894. global $conf;
  895. $err = 0;
  896. // Common module
  897. $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
  898. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  899. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  900. $sql .= " AND entity IN (0, ".$entity.")";
  901. dol_syslog(get_class($this)."::_unactive", LOG_DEBUG);
  902. $this->db->query($sql);
  903. return $err;
  904. }
  905. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps,PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  906. /**
  907. * Create tables and keys required by module:
  908. * - Files table.sql or table-module.sql with create table instructions
  909. * - Then table.key.sql or table-module.key.sql with create keys instructions
  910. * - Then data_xxx.sql (usualy provided by external modules only)
  911. * - Then update_xxx.sql (usualy provided by external modules only)
  912. * Files must be stored in subdirectory 'tables' or 'data' into directory $reldir (Example: '/install/mysql/' or '/module/sql/')
  913. * This function may also be called by :
  914. * - _load_tables('/install/mysql/', 'modulename') into the this->init() of core module descriptors.
  915. * - _load_tables('/mymodule/sql/') into the this->init() of external module descriptors.
  916. *
  917. * @param string $reldir Relative directory where to scan files. Example: '/install/mysql/' or '/module/sql/'
  918. * @param string $onlywithsuffix Only with the defined suffix
  919. * @return int <=0 if KO, >0 if OK
  920. */
  921. protected function _load_tables($reldir, $onlywithsuffix = '')
  922. {
  923. // phpcs:enable
  924. global $conf;
  925. $error = 0;
  926. $dirfound = 0;
  927. if (empty($reldir)) {
  928. return 1;
  929. }
  930. include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  931. $ok = 1;
  932. foreach ($conf->file->dol_document_root as $dirroot) {
  933. if ($ok) {
  934. $dirsql = $dirroot.$reldir;
  935. $ok = 0;
  936. // We will loop on xxx/, xxx/tables/, xxx/data/
  937. $listofsubdir = array('', 'tables/', 'data/');
  938. if ($this->db->type == 'pgsql') {
  939. $listofsubdir[] = '../pgsql/functions/';
  940. }
  941. foreach ($listofsubdir as $subdir) {
  942. $dir = $dirsql.$subdir;
  943. $handle = @opendir($dir); // Dir may not exists
  944. if (is_resource($handle)) {
  945. $dirfound++;
  946. // Run llx_mytable.sql files, then llx_mytable_*.sql
  947. $files = array();
  948. while (($file = readdir($handle)) !== false) {
  949. $files[] = $file;
  950. }
  951. sort($files);
  952. foreach ($files as $file) {
  953. if ($onlywithsuffix) {
  954. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  955. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  956. continue;
  957. } else {
  958. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  959. }
  960. }
  961. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
  962. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  963. if ($result <= 0) {
  964. $error++;
  965. }
  966. }
  967. }
  968. rewinddir($handle);
  969. // Run llx_mytable.key.sql files (Must be done after llx_mytable.sql) then then llx_mytable_*.key.sql
  970. $files = array();
  971. while (($file = readdir($handle)) !== false) {
  972. $files[] = $file;
  973. }
  974. sort($files);
  975. foreach ($files as $file) {
  976. if ($onlywithsuffix) {
  977. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  978. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  979. continue;
  980. } else {
  981. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  982. }
  983. }
  984. if (preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
  985. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  986. if ($result <= 0) {
  987. $error++;
  988. }
  989. }
  990. }
  991. rewinddir($handle);
  992. // Run functions-xxx.sql files (Must be done after llx_mytable.key.sql)
  993. $files = array();
  994. while (($file = readdir($handle)) !== false) {
  995. $files[] = $file;
  996. }
  997. sort($files);
  998. foreach ($files as $file) {
  999. if ($onlywithsuffix) {
  1000. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  1001. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  1002. continue;
  1003. } else {
  1004. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  1005. }
  1006. }
  1007. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 9) == 'functions') {
  1008. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  1009. if ($result <= 0) {
  1010. $error++;
  1011. }
  1012. }
  1013. }
  1014. rewinddir($handle);
  1015. // Run data_xxx.sql files (Must be done after llx_mytable.key.sql)
  1016. $files = array();
  1017. while (($file = readdir($handle)) !== false) {
  1018. $files[] = $file;
  1019. }
  1020. sort($files);
  1021. foreach ($files as $file) {
  1022. if ($onlywithsuffix) {
  1023. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  1024. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  1025. continue;
  1026. } else {
  1027. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  1028. }
  1029. }
  1030. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'data') {
  1031. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  1032. if ($result <= 0) {
  1033. $error++;
  1034. }
  1035. }
  1036. }
  1037. rewinddir($handle);
  1038. // Run update_xxx.sql files
  1039. $files = array();
  1040. while (($file = readdir($handle)) !== false) {
  1041. $files[] = $file;
  1042. }
  1043. sort($files);
  1044. foreach ($files as $file) {
  1045. if ($onlywithsuffix) {
  1046. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  1047. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  1048. continue;
  1049. } else {
  1050. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  1051. }
  1052. }
  1053. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 6) == 'update') {
  1054. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  1055. if ($result <= 0) {
  1056. $error++;
  1057. }
  1058. }
  1059. }
  1060. closedir($handle);
  1061. }
  1062. }
  1063. if ($error == 0) {
  1064. $ok = 1;
  1065. }
  1066. }
  1067. }
  1068. if (!$dirfound) {
  1069. dol_syslog("A module ask to load sql files into ".$reldir." but this directory was not found.", LOG_WARNING);
  1070. }
  1071. return $ok;
  1072. }
  1073. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1074. /**
  1075. * Adds boxes
  1076. *
  1077. * @param string $option Options when disabling module ('newboxdefonly'=insert only boxes definition)
  1078. *
  1079. * @return int Error count (0 if OK)
  1080. */
  1081. public function insert_boxes($option = '')
  1082. {
  1083. // phpcs:enable
  1084. include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
  1085. global $conf;
  1086. $err = 0;
  1087. if (is_array($this->boxes)) {
  1088. dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
  1089. $pos_name = InfoBox::getListOfPagesForBoxes();
  1090. foreach ($this->boxes as $key => $value) {
  1091. $file = isset($this->boxes[$key]['file']) ? $this->boxes[$key]['file'] : '';
  1092. $note = isset($this->boxes[$key]['note']) ? $this->boxes[$key]['note'] : '';
  1093. $enabledbydefaulton = isset($this->boxes[$key]['enabledbydefaulton']) ? $this->boxes[$key]['enabledbydefaulton'] : 'Home';
  1094. if (empty($file)) {
  1095. $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
  1096. }
  1097. if (empty($note)) {
  1098. $note = isset($this->boxes[$key][2]) ? $this->boxes[$key][2] : ''; // For backward compatibility
  1099. }
  1100. // Search if boxes def already present
  1101. $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
  1102. $sql .= " WHERE file = '".$this->db->escape($file)."'";
  1103. $sql .= " AND entity = ".$conf->entity;
  1104. if ($note) {
  1105. $sql .= " AND note ='".$this->db->escape($note)."'";
  1106. }
  1107. $result = $this->db->query($sql);
  1108. if ($result) {
  1109. $obj = $this->db->fetch_object($result);
  1110. if ($obj->nb == 0) {
  1111. $this->db->begin();
  1112. if (!$err) {
  1113. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, entity, note)";
  1114. $sql .= " VALUES ('".$this->db->escape($file)."', ";
  1115. $sql .= $conf->entity.", ";
  1116. $sql .= $note ? "'".$this->db->escape($note)."'" : "null";
  1117. $sql .= ")";
  1118. dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
  1119. $resql = $this->db->query($sql);
  1120. if (!$resql) {
  1121. $err++;
  1122. }
  1123. }
  1124. if (!$err && !preg_match('/newboxdefonly/', $option)) {
  1125. $lastid = $this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def", "rowid");
  1126. foreach ($pos_name as $key2 => $val2) {
  1127. //print 'key2='.$key2.'-val2='.$val2."<br>\n";
  1128. if ($enabledbydefaulton && $val2 != $enabledbydefaulton) {
  1129. continue; // Not enabled by default onto this page.
  1130. }
  1131. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id, position, box_order, fk_user, entity)";
  1132. $sql .= " VALUES (".((int) $lastid).", ".((int) $key2).", '0', 0, ".((int) $conf->entity).")";
  1133. dol_syslog(get_class($this)."::insert_boxes onto page ".$key2."=".$val2, LOG_DEBUG);
  1134. $resql = $this->db->query($sql);
  1135. if (!$resql) {
  1136. $err++;
  1137. }
  1138. }
  1139. }
  1140. if (!$err) {
  1141. $this->db->commit();
  1142. } else {
  1143. $this->error = $this->db->lasterror();
  1144. $this->db->rollback();
  1145. }
  1146. }
  1147. // else box already registered into database
  1148. } else {
  1149. $this->error = $this->db->lasterror();
  1150. $err++;
  1151. }
  1152. }
  1153. }
  1154. return $err;
  1155. }
  1156. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1157. /**
  1158. * Removes boxes
  1159. *
  1160. * @return int Error count (0 if OK)
  1161. */
  1162. public function delete_boxes()
  1163. {
  1164. // phpcs:enable
  1165. global $conf;
  1166. $err = 0;
  1167. if (is_array($this->boxes)) {
  1168. foreach ($this->boxes as $key => $value) {
  1169. //$titre = $this->boxes[$key][0];
  1170. if (empty($this->boxes[$key]['file'])) {
  1171. $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
  1172. } else {
  1173. $file = $this->boxes[$key]['file'];
  1174. }
  1175. //$note = $this->boxes[$key][2];
  1176. // TODO If the box is also included by another module and the other module is still on, we should not remove it.
  1177. // For the moment, we manage this with hard coded exception
  1178. //print "Remove box ".$file.'<br>';
  1179. if ($file == 'box_graph_product_distribution.php') {
  1180. if (isModEnabled("product") || isModEnabled("service")) {
  1181. dol_syslog("We discard deleting module ".$file." because another module still active requires it.");
  1182. continue;
  1183. }
  1184. }
  1185. if ($this->db->type == 'sqlite3') {
  1186. // sqlite doesn't support "USING" syntax.
  1187. // TODO: remove this dependency.
  1188. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes ";
  1189. $sql .= "WHERE ".MAIN_DB_PREFIX."boxes.box_id IN (";
  1190. $sql .= "SELECT ".MAIN_DB_PREFIX."boxes_def.rowid ";
  1191. $sql .= "FROM ".MAIN_DB_PREFIX."boxes_def ";
  1192. $sql .= "WHERE ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."') ";
  1193. $sql .= "AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
  1194. } else {
  1195. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  1196. $sql .= " USING ".MAIN_DB_PREFIX."boxes, ".MAIN_DB_PREFIX."boxes_def";
  1197. $sql .= " WHERE ".MAIN_DB_PREFIX."boxes.box_id = ".MAIN_DB_PREFIX."boxes_def.rowid";
  1198. $sql .= " AND ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."'";
  1199. $sql .= " AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
  1200. }
  1201. dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
  1202. $resql = $this->db->query($sql);
  1203. if (!$resql) {
  1204. $this->error = $this->db->lasterror();
  1205. $err++;
  1206. }
  1207. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
  1208. $sql .= " WHERE file = '".$this->db->escape($file)."'";
  1209. $sql .= " AND entity = ".$conf->entity; // Do not use getEntity here, we want to delete only in current company
  1210. dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
  1211. $resql = $this->db->query($sql);
  1212. if (!$resql) {
  1213. $this->error = $this->db->lasterror();
  1214. $err++;
  1215. }
  1216. }
  1217. }
  1218. return $err;
  1219. }
  1220. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1221. /**
  1222. * Adds cronjobs
  1223. *
  1224. * @return int Error count (0 if OK)
  1225. */
  1226. public function insert_cronjobs()
  1227. {
  1228. // phpcs:enable
  1229. include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
  1230. global $conf;
  1231. $err = 0;
  1232. if (is_array($this->cronjobs)) {
  1233. dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG);
  1234. foreach ($this->cronjobs as $key => $value) {
  1235. $entity = isset($this->cronjobs[$key]['entity']) ? $this->cronjobs[$key]['entity'] : $conf->entity;
  1236. $label = isset($this->cronjobs[$key]['label']) ? $this->cronjobs[$key]['label'] : '';
  1237. $jobtype = isset($this->cronjobs[$key]['jobtype']) ? $this->cronjobs[$key]['jobtype'] : '';
  1238. $class = isset($this->cronjobs[$key]['class']) ? $this->cronjobs[$key]['class'] : '';
  1239. $objectname = isset($this->cronjobs[$key]['objectname']) ? $this->cronjobs[$key]['objectname'] : '';
  1240. $method = isset($this->cronjobs[$key]['method']) ? $this->cronjobs[$key]['method'] : '';
  1241. $command = isset($this->cronjobs[$key]['command']) ? $this->cronjobs[$key]['command'] : '';
  1242. $parameters = isset($this->cronjobs[$key]['parameters']) ? $this->cronjobs[$key]['parameters'] : '';
  1243. $comment = isset($this->cronjobs[$key]['comment']) ? $this->cronjobs[$key]['comment'] : '';
  1244. $frequency = isset($this->cronjobs[$key]['frequency']) ? $this->cronjobs[$key]['frequency'] : '';
  1245. $unitfrequency = isset($this->cronjobs[$key]['unitfrequency']) ? $this->cronjobs[$key]['unitfrequency'] : '';
  1246. $priority = isset($this->cronjobs[$key]['priority']) ? $this->cronjobs[$key]['priority'] : '';
  1247. $datestart = isset($this->cronjobs[$key]['datestart']) ? $this->cronjobs[$key]['datestart'] : '';
  1248. $dateend = isset($this->cronjobs[$key]['dateend']) ? $this->cronjobs[$key]['dateend'] : '';
  1249. $status = isset($this->cronjobs[$key]['status']) ? $this->cronjobs[$key]['status'] : '';
  1250. $test = isset($this->cronjobs[$key]['test']) ? $this->cronjobs[$key]['test'] : ''; // Line must be enabled or not (so visible or not)
  1251. // Search if cron entry already present
  1252. $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob";
  1253. //$sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
  1254. $sql .= " WHERE label = '".$this->db->escape($label)."'";
  1255. /*if ($class) {
  1256. $sql .= " AND classesname = '".$this->db->escape($class)."'";
  1257. }
  1258. if ($objectname) {
  1259. $sql .= " AND objectname = '".$this->db->escape($objectname)."'";
  1260. }
  1261. if ($method) {
  1262. $sql .= " AND methodename = '".$this->db->escape($method)."'";
  1263. }
  1264. if ($command) {
  1265. $sql .= " AND command = '".$this->db->escape($command)."'";
  1266. }
  1267. if ($parameters) {
  1268. $sql .= " AND params = '".$this->db->escape($parameters)."'";
  1269. }*/
  1270. $sql .= " AND entity = ".((int) $entity); // Must be exact entity
  1271. $now = dol_now();
  1272. $result = $this->db->query($sql);
  1273. if ($result) {
  1274. $obj = $this->db->fetch_object($result);
  1275. if ($obj->nb == 0) {
  1276. $this->db->begin();
  1277. if (!$err) {
  1278. $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, datestart, dateend, label, jobtype, classesname, objectname, methodename, command, params, note,";
  1279. if (is_int($frequency)) {
  1280. $sql .= ' frequency,';
  1281. }
  1282. if (is_int($unitfrequency)) {
  1283. $sql .= ' unitfrequency,';
  1284. }
  1285. if (is_int($priority)) {
  1286. $sql .= ' priority,';
  1287. }
  1288. if (is_int($status)) {
  1289. $sql .= ' status,';
  1290. }
  1291. $sql .= " entity, test)";
  1292. $sql .= " VALUES (";
  1293. $sql .= "'".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."', ";
  1294. $sql .= "'".$this->db->idate($now)."', ";
  1295. $sql .= ($datestart ? "'".$this->db->idate($datestart)."'" : "'".$this->db->idate($now)."'").", ";
  1296. $sql .= ($dateend ? "'".$this->db->idate($dateend)."'" : "NULL").", ";
  1297. $sql .= "'".$this->db->escape($label)."', ";
  1298. $sql .= "'".$this->db->escape($jobtype)."', ";
  1299. $sql .= ($class ? "'".$this->db->escape($class)."'" : "null").",";
  1300. $sql .= ($objectname ? "'".$this->db->escape($objectname)."'" : "null").",";
  1301. $sql .= ($method ? "'".$this->db->escape($method)."'" : "null").",";
  1302. $sql .= ($command ? "'".$this->db->escape($command)."'" : "null").",";
  1303. $sql .= ($parameters ? "'".$this->db->escape($parameters)."'" : "null").",";
  1304. $sql .= ($comment ? "'".$this->db->escape($comment)."'" : "null").",";
  1305. if (is_int($frequency)) {
  1306. $sql .= "'".$this->db->escape($frequency)."', ";
  1307. }
  1308. if (is_int($unitfrequency)) {
  1309. $sql .= "'".$this->db->escape($unitfrequency)."', ";
  1310. }
  1311. if (is_int($priority)) {
  1312. $sql .= "'".$this->db->escape($priority)."', ";
  1313. }
  1314. if (is_int($status)) {
  1315. $sql .= ((int) $status).", ";
  1316. }
  1317. $sql .= $entity.",";
  1318. $sql .= "'".$this->db->escape($test)."'";
  1319. $sql .= ")";
  1320. $resql = $this->db->query($sql);
  1321. if (!$resql) {
  1322. $err++;
  1323. }
  1324. }
  1325. if (!$err) {
  1326. $this->db->commit();
  1327. } else {
  1328. $this->error = $this->db->lasterror();
  1329. $this->db->rollback();
  1330. }
  1331. }
  1332. // else box already registered into database
  1333. } else {
  1334. $this->error = $this->db->lasterror();
  1335. $err++;
  1336. }
  1337. }
  1338. }
  1339. return $err;
  1340. }
  1341. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1342. /**
  1343. * Removes boxes
  1344. *
  1345. * @return int Error count (0 if OK)
  1346. */
  1347. public function delete_cronjobs()
  1348. {
  1349. // phpcs:enable
  1350. global $conf;
  1351. $err = 0;
  1352. if (is_array($this->cronjobs)) {
  1353. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
  1354. $sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
  1355. $sql .= " AND entity = ".$conf->entity;
  1356. $sql .= " AND test = '1'"; // We delete on lines that are not set with a complete test that is '$conf->module->enabled' so when module is disabled, the cron is also removed.
  1357. // For crons declared with a '$conf->module->enabled', there is no need to delete the line, so we don't loose setup if we reenable module.
  1358. dol_syslog(get_class($this)."::delete_cronjobs", LOG_DEBUG);
  1359. $resql = $this->db->query($sql);
  1360. if (!$resql) {
  1361. $this->error = $this->db->lasterror();
  1362. $err++;
  1363. }
  1364. }
  1365. return $err;
  1366. }
  1367. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1368. /**
  1369. * Removes tabs
  1370. *
  1371. * @return int Error count (0 if OK)
  1372. */
  1373. public function delete_tabs()
  1374. {
  1375. // phpcs:enable
  1376. global $conf;
  1377. $err = 0;
  1378. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1379. $sql .= " WHERE ".$this->db->decrypt('name')." like '".$this->db->escape($this->const_name)."_TABS_%'";
  1380. $sql .= " AND entity = ".$conf->entity;
  1381. dol_syslog(get_class($this)."::delete_tabs", LOG_DEBUG);
  1382. if (!$this->db->query($sql)) {
  1383. $this->error = $this->db->lasterror();
  1384. $err++;
  1385. }
  1386. return $err;
  1387. }
  1388. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1389. /**
  1390. * Adds tabs
  1391. *
  1392. * @return int Error count (0 if ok)
  1393. */
  1394. public function insert_tabs()
  1395. {
  1396. // phpcs:enable
  1397. global $conf;
  1398. $err = 0;
  1399. if (!empty($this->tabs)) {
  1400. dol_syslog(get_class($this)."::insert_tabs", LOG_DEBUG);
  1401. $i = 0;
  1402. foreach ($this->tabs as $key => $value) {
  1403. if (is_array($value) && count($value) == 0) {
  1404. continue; // Discard empty arrays
  1405. }
  1406. $entity = $conf->entity;
  1407. $newvalue = $value;
  1408. if (is_array($value)) {
  1409. $newvalue = $value['data'];
  1410. if (isset($value['entity'])) {
  1411. $entity = $value['entity'];
  1412. }
  1413. }
  1414. if ($newvalue) {
  1415. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
  1416. $sql .= "name";
  1417. $sql .= ", type";
  1418. $sql .= ", value";
  1419. $sql .= ", note";
  1420. $sql .= ", visible";
  1421. $sql .= ", entity";
  1422. $sql .= ")";
  1423. $sql .= " VALUES (";
  1424. $sql .= $this->db->encrypt($this->const_name."_TABS_".$i);
  1425. $sql .= ", 'chaine'";
  1426. $sql .= ", ".$this->db->encrypt($newvalue);
  1427. $sql .= ", null";
  1428. $sql .= ", '0'";
  1429. $sql .= ", ".((int) $entity);
  1430. $sql .= ")";
  1431. $resql = $this->db->query($sql);
  1432. if (!$resql) {
  1433. dol_syslog($this->db->lasterror(), LOG_ERR);
  1434. if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  1435. $this->error = $this->db->lasterror();
  1436. $this->errors[] = $this->db->lasterror();
  1437. $err++;
  1438. break;
  1439. }
  1440. }
  1441. }
  1442. $i++;
  1443. }
  1444. }
  1445. return $err;
  1446. }
  1447. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1448. /**
  1449. * Adds constants
  1450. *
  1451. * @return int Error count (0 if OK)
  1452. */
  1453. public function insert_const()
  1454. {
  1455. // phpcs:enable
  1456. global $conf;
  1457. $err = 0;
  1458. if (empty($this->const)) {
  1459. return 0;
  1460. }
  1461. dol_syslog(get_class($this)."::insert_const", LOG_DEBUG);
  1462. foreach ($this->const as $key => $value) {
  1463. $name = $this->const[$key][0];
  1464. $type = $this->const[$key][1];
  1465. $val = $this->const[$key][2];
  1466. $note = isset($this->const[$key][3]) ? $this->const[$key][3] : '';
  1467. $visible = isset($this->const[$key][4]) ? $this->const[$key][4] : 0;
  1468. $entity = (!empty($this->const[$key][5]) && $this->const[$key][5] != 'current') ? 0 : $conf->entity;
  1469. // Clean
  1470. if (empty($visible)) {
  1471. $visible = '0';
  1472. }
  1473. if (empty($val) && $val != '0') {
  1474. $val = '';
  1475. }
  1476. $sql = "SELECT count(*)";
  1477. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  1478. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
  1479. $sql .= " AND entity = ".((int) $entity);
  1480. $result = $this->db->query($sql);
  1481. if ($result) {
  1482. $row = $this->db->fetch_row($result);
  1483. if ($row[0] == 0) { // If not found
  1484. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,type,value,note,visible,entity)";
  1485. $sql .= " VALUES (";
  1486. $sql .= $this->db->encrypt($name);
  1487. $sql .= ",'".$this->db->escape($type)."'";
  1488. $sql .= ",".(($val != '') ? $this->db->encrypt($val) : "''");
  1489. $sql .= ",".($note ? "'".$this->db->escape($note)."'" : "null");
  1490. $sql .= ",'".$this->db->escape($visible)."'";
  1491. $sql .= ",".$entity;
  1492. $sql .= ")";
  1493. if (!$this->db->query($sql)) {
  1494. $err++;
  1495. }
  1496. } else {
  1497. dol_syslog(get_class($this)."::insert_const constant '".$name."' already exists", LOG_WARNING);
  1498. }
  1499. } else {
  1500. $err++;
  1501. }
  1502. }
  1503. return $err;
  1504. }
  1505. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1506. /**
  1507. * Removes constants tagged 'deleteonunactive'
  1508. *
  1509. * @return int <0 if KO, 0 if OK
  1510. */
  1511. public function delete_const()
  1512. {
  1513. // phpcs:enable
  1514. global $conf;
  1515. $err = 0;
  1516. if (empty($this->const)) {
  1517. return 0;
  1518. }
  1519. foreach ($this->const as $key => $value) {
  1520. $name = $this->const[$key][0];
  1521. $deleteonunactive = (!empty($this->const[$key][6])) ? 1 : 0;
  1522. if ($deleteonunactive) {
  1523. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1524. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
  1525. $sql .= " AND entity in (0, ".$conf->entity.")";
  1526. dol_syslog(get_class($this)."::delete_const", LOG_DEBUG);
  1527. if (!$this->db->query($sql)) {
  1528. $this->error = $this->db->lasterror();
  1529. $err++;
  1530. }
  1531. }
  1532. }
  1533. return $err;
  1534. }
  1535. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1536. /**
  1537. * Adds access rights
  1538. *
  1539. * @param int $reinitadminperms If 1, we also grant them to all admin users
  1540. * @param int $force_entity Force current entity
  1541. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1542. * @return int Error count (0 if OK)
  1543. */
  1544. public function insert_permissions($reinitadminperms = 0, $force_entity = null, $notrigger = 0)
  1545. {
  1546. // phpcs:enable
  1547. global $conf, $user;
  1548. $err = 0;
  1549. $entity = (!empty($force_entity) ? $force_entity : $conf->entity);
  1550. dol_syslog(get_class($this)."::insert_permissions", LOG_DEBUG);
  1551. // Test if module is activated
  1552. $sql_del = "SELECT ".$this->db->decrypt('value')." as value";
  1553. $sql_del .= " FROM ".MAIN_DB_PREFIX."const";
  1554. $sql_del .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  1555. $sql_del .= " AND entity IN (0,".$entity.")";
  1556. $resql = $this->db->query($sql_del);
  1557. if ($resql) {
  1558. $obj = $this->db->fetch_object($resql);
  1559. if ($obj !== null && !empty($obj->value) && !empty($this->rights)) {
  1560. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1561. // If the module is active
  1562. foreach ($this->rights as $key => $value) {
  1563. $r_id = $this->rights[$key][0]; // permission id in llx_rights_def (not unique because primary key is couple id-entity)
  1564. $r_desc = $this->rights[$key][1];
  1565. $r_type = isset($this->rights[$key][2]) ? $this->rights[$key][2] : '';
  1566. $r_def = empty($this->rights[$key][3]) ? 0 : $this->rights[$key][3];
  1567. $r_perms = $this->rights[$key][4];
  1568. $r_subperms = isset($this->rights[$key][5]) ? $this->rights[$key][5] : '';
  1569. $r_modul = empty($this->rights_class) ? strtolower($this->name) : $this->rights_class;
  1570. if (empty($r_type)) {
  1571. $r_type = 'w';
  1572. }
  1573. // Search if perm already present
  1574. $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."rights_def";
  1575. $sql .= " WHERE id = ".((int) $r_id)." AND entity = ".((int) $entity);
  1576. $resqlselect = $this->db->query($sql);
  1577. if ($resqlselect) {
  1578. $objcount = $this->db->fetch_object($resqlselect);
  1579. if ($objcount && $objcount->nb == 0) {
  1580. if (dol_strlen($r_perms)) {
  1581. if (dol_strlen($r_subperms)) {
  1582. $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
  1583. $sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms)";
  1584. $sql .= " VALUES ";
  1585. $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.",'".$this->db->escape($r_perms)."','".$this->db->escape($r_subperms)."')";
  1586. } else {
  1587. $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
  1588. $sql .= " (id, entity, libelle, module, type, bydefault, perms)";
  1589. $sql .= " VALUES ";
  1590. $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.",'".$this->db->escape($r_perms)."')";
  1591. }
  1592. } else {
  1593. $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def ";
  1594. $sql .= " (id, entity, libelle, module, type, bydefault)";
  1595. $sql .= " VALUES ";
  1596. $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.")";
  1597. }
  1598. $resqlinsert = $this->db->query($sql, 1);
  1599. if (!$resqlinsert) {
  1600. if ($this->db->errno() != "DB_ERROR_RECORD_ALREADY_EXISTS") {
  1601. $this->error = $this->db->lasterror();
  1602. $err++;
  1603. break;
  1604. } else {
  1605. dol_syslog(get_class($this)."::insert_permissions record already exists", LOG_INFO);
  1606. }
  1607. }
  1608. $this->db->free($resqlinsert);
  1609. }
  1610. $this->db->free($resqlselect);
  1611. }
  1612. // If we want to init permissions on admin users
  1613. if ($reinitadminperms) {
  1614. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."user WHERE admin = 1";
  1615. dol_syslog(get_class($this)."::insert_permissions Search all admin users", LOG_DEBUG);
  1616. $resqlseladmin = $this->db->query($sql, 1);
  1617. if ($resqlseladmin) {
  1618. $num = $this->db->num_rows($resqlseladmin);
  1619. $i = 0;
  1620. while ($i < $num) {
  1621. $obj2 = $this->db->fetch_object($resqlseladmin);
  1622. dol_syslog(get_class($this)."::insert_permissions Add permission id ".$r_id." to user id=".$obj2->rowid);
  1623. $tmpuser = new User($this->db);
  1624. $result = $tmpuser->fetch($obj2->rowid);
  1625. if ($result > 0) {
  1626. $tmpuser->addrights($r_id, '', '', 0, 1);
  1627. } else {
  1628. dol_syslog(get_class($this)."::insert_permissions Failed to add the permission to user because fetch return an error", LOG_ERR);
  1629. }
  1630. $i++;
  1631. }
  1632. } else {
  1633. dol_print_error($this->db);
  1634. }
  1635. }
  1636. }
  1637. if ($reinitadminperms && !empty($user->admin)) { // Reload permission for current user if defined
  1638. // We reload permissions
  1639. $user->clearrights();
  1640. $user->getrights();
  1641. }
  1642. }
  1643. $this->db->free($resql);
  1644. } else {
  1645. $this->error = $this->db->lasterror();
  1646. $err++;
  1647. }
  1648. return $err;
  1649. }
  1650. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1651. /**
  1652. * Removes access rights
  1653. *
  1654. * @return int Error count (0 if OK)
  1655. */
  1656. public function delete_permissions()
  1657. {
  1658. // phpcs:enable
  1659. global $conf;
  1660. $err = 0;
  1661. $sql = "DELETE FROM ".MAIN_DB_PREFIX."rights_def";
  1662. $sql .= " WHERE module = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
  1663. $sql .= " AND entity = ".$conf->entity;
  1664. dol_syslog(get_class($this)."::delete_permissions", LOG_DEBUG);
  1665. if (!$this->db->query($sql)) {
  1666. $this->error = $this->db->lasterror();
  1667. $err++;
  1668. }
  1669. return $err;
  1670. }
  1671. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1672. /**
  1673. * Adds menu entries
  1674. *
  1675. * @return int Error count (0 if OK)
  1676. */
  1677. public function insert_menus()
  1678. {
  1679. // phpcs:enable
  1680. global $user;
  1681. if (!is_array($this->menu) || empty($this->menu)) {
  1682. return 0;
  1683. }
  1684. include_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
  1685. dol_syslog(get_class($this)."::insert_menus", LOG_DEBUG);
  1686. $err = 0;
  1687. $this->db->begin();
  1688. foreach ($this->menu as $key => $value) {
  1689. $menu = new Menubase($this->db);
  1690. $menu->menu_handler = 'all';
  1691. //$menu->module=strtolower($this->name); TODO When right_class will be same than module name
  1692. $menu->module = (empty($this->rights_class) ? strtolower($this->name) : $this->rights_class);
  1693. if (!$this->menu[$key]['fk_menu']) {
  1694. $menu->fk_menu = 0;
  1695. } else {
  1696. $foundparent = 0;
  1697. $fk_parent = $this->menu[$key]['fk_menu'];
  1698. $reg = array();
  1699. if (preg_match('/^r=/', $fk_parent)) { // old deprecated method
  1700. $fk_parent = str_replace('r=', '', $fk_parent);
  1701. if (isset($this->menu[$fk_parent]['rowid'])) {
  1702. $menu->fk_menu = $this->menu[$fk_parent]['rowid'];
  1703. $foundparent = 1;
  1704. }
  1705. } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+),fk_leftmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
  1706. $menu->fk_menu = -1;
  1707. $menu->fk_mainmenu = $reg[1];
  1708. $menu->fk_leftmenu = $reg[2];
  1709. $foundparent = 1;
  1710. } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
  1711. $menu->fk_menu = -1;
  1712. $menu->fk_mainmenu = $reg[1];
  1713. $menu->fk_leftmenu = '';
  1714. $foundparent = 1;
  1715. }
  1716. if (!$foundparent) {
  1717. $this->error = "ErrorBadDefinitionOfMenuArrayInModuleDescriptor";
  1718. dol_syslog(get_class($this)."::insert_menus ".$this->error." ".$this->menu[$key]['fk_menu'], LOG_ERR);
  1719. $err++;
  1720. }
  1721. }
  1722. $menu->type = $this->menu[$key]['type'];
  1723. $menu->mainmenu = isset($this->menu[$key]['mainmenu']) ? $this->menu[$key]['mainmenu'] : (isset($menu->fk_mainmenu) ? $menu->fk_mainmenu : '');
  1724. $menu->leftmenu = isset($this->menu[$key]['leftmenu']) ? $this->menu[$key]['leftmenu'] : '';
  1725. $menu->title = $this->menu[$key]['titre'];
  1726. $menu->prefix = isset($this->menu[$key]['prefix']) ? $this->menu[$key]['prefix'] : '';
  1727. $menu->url = $this->menu[$key]['url'];
  1728. $menu->langs = isset($this->menu[$key]['langs']) ? $this->menu[$key]['langs'] : '';
  1729. $menu->position = $this->menu[$key]['position'];
  1730. $menu->perms = $this->menu[$key]['perms'];
  1731. $menu->target = isset($this->menu[$key]['target']) ? $this->menu[$key]['target'] : '';
  1732. $menu->user = $this->menu[$key]['user'];
  1733. $menu->enabled = isset($this->menu[$key]['enabled']) ? $this->menu[$key]['enabled'] : 0;
  1734. $menu->position = $this->menu[$key]['position'];
  1735. if (!$err) {
  1736. $result = $menu->create($user); // Save menu entry into table llx_menu
  1737. if ($result > 0) {
  1738. $this->menu[$key]['rowid'] = $result;
  1739. } else {
  1740. $this->error = $menu->error;
  1741. dol_syslog(get_class($this).'::insert_menus result='.$result." ".$this->error, LOG_ERR);
  1742. $err++;
  1743. break;
  1744. }
  1745. }
  1746. }
  1747. if (!$err) {
  1748. $this->db->commit();
  1749. } else {
  1750. dol_syslog(get_class($this)."::insert_menus ".$this->error, LOG_ERR);
  1751. $this->db->rollback();
  1752. }
  1753. return $err;
  1754. }
  1755. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1756. /**
  1757. * Removes menu entries
  1758. *
  1759. * @return int Error count (0 if OK)
  1760. */
  1761. public function delete_menus()
  1762. {
  1763. // phpcs:enable
  1764. global $conf;
  1765. $err = 0;
  1766. //$module=strtolower($this->name); TODO When right_class will be same than module name
  1767. $module = empty($this->rights_class) ?strtolower($this->name) : $this->rights_class;
  1768. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  1769. $sql .= " WHERE module = '".$this->db->escape($module)."'";
  1770. $sql .= " AND entity = ".$conf->entity;
  1771. dol_syslog(get_class($this)."::delete_menus", LOG_DEBUG);
  1772. $resql = $this->db->query($sql);
  1773. if (!$resql) {
  1774. $this->error = $this->db->lasterror();
  1775. $err++;
  1776. }
  1777. return $err;
  1778. }
  1779. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1780. /**
  1781. * Creates directories
  1782. *
  1783. * @return int Error count (0 if OK)
  1784. */
  1785. public function create_dirs()
  1786. {
  1787. // phpcs:enable
  1788. global $langs, $conf;
  1789. $err = 0;
  1790. if (isset($this->dirs) && is_array($this->dirs)) {
  1791. foreach ($this->dirs as $key => $value) {
  1792. $addtodatabase = 0;
  1793. if (!is_array($value)) {
  1794. $dir = $value; // Default simple mode
  1795. } else {
  1796. $constname = $this->const_name."_DIR_";
  1797. $dir = $this->dirs[$key][1];
  1798. $addtodatabase = empty($this->dirs[$key][2]) ? '' : $this->dirs[$key][2]; // Create constante in llx_const
  1799. $subname = empty($this->dirs[$key][3]) ? '' : strtoupper($this->dirs[$key][3]); // Add submodule name (ex: $conf->module->submodule->dir_output)
  1800. $forcename = empty($this->dirs[$key][4]) ? '' : strtoupper($this->dirs[$key][4]); // Change the module name if different
  1801. if (!empty($forcename)) {
  1802. $constname = 'MAIN_MODULE_'.$forcename."_DIR_";
  1803. }
  1804. if (!empty($subname)) {
  1805. $constname = $constname.$subname."_";
  1806. }
  1807. $name = $constname.strtoupper($this->dirs[$key][0]);
  1808. }
  1809. // Define directory full path ($dir must start with "/")
  1810. if (empty($conf->global->MAIN_MODULE_MULTICOMPANY) || $conf->entity == 1) {
  1811. $fulldir = DOL_DATA_ROOT.$dir;
  1812. } else {
  1813. $fulldir = DOL_DATA_ROOT."/".$conf->entity.$dir;
  1814. }
  1815. // Create dir if it does not exists
  1816. if (!empty($fulldir) && !file_exists($fulldir)) {
  1817. if (dol_mkdir($fulldir, DOL_DATA_ROOT) < 0) {
  1818. $this->error = $langs->trans("ErrorCanNotCreateDir", $fulldir);
  1819. dol_syslog(get_class($this)."::_init ".$this->error, LOG_ERR);
  1820. $err++;
  1821. }
  1822. }
  1823. // Define the constant in database if requested (not the default mode)
  1824. if (!empty($addtodatabase)) {
  1825. $result = $this->insert_dirs($name, $dir);
  1826. if ($result) {
  1827. $err++;
  1828. }
  1829. }
  1830. }
  1831. }
  1832. return $err;
  1833. }
  1834. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1835. /**
  1836. * Adds directories definitions
  1837. *
  1838. * @param string $name Name
  1839. * @param string $dir Directory
  1840. *
  1841. * @return int Error count (0 if OK)
  1842. */
  1843. public function insert_dirs($name, $dir)
  1844. {
  1845. // phpcs:enable
  1846. global $conf;
  1847. $err = 0;
  1848. $sql = "SELECT count(*)";
  1849. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  1850. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
  1851. $sql .= " AND entity = ".$conf->entity;
  1852. dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
  1853. $result = $this->db->query($sql);
  1854. if ($result) {
  1855. $row = $this->db->fetch_row($result);
  1856. if ($row[0] == 0) {
  1857. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, type, value, note, visible, entity)";
  1858. $sql .= " VALUES (".$this->db->encrypt($name).", 'chaine', ".$this->db->encrypt($dir).", '".$this->db->escape("Directory for module ".$this->name)."', '0', ".((int) $conf->entity).")";
  1859. dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
  1860. $this->db->query($sql);
  1861. }
  1862. } else {
  1863. $this->error = $this->db->lasterror();
  1864. $err++;
  1865. }
  1866. return $err;
  1867. }
  1868. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1869. /**
  1870. * Removes directories
  1871. *
  1872. * @return int Error count (0 if OK)
  1873. */
  1874. public function delete_dirs()
  1875. {
  1876. // phpcs:enable
  1877. global $conf;
  1878. $err = 0;
  1879. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1880. $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_DIR_%'";
  1881. $sql .= " AND entity = ".$conf->entity;
  1882. dol_syslog(get_class($this)."::delete_dirs", LOG_DEBUG);
  1883. if (!$this->db->query($sql)) {
  1884. $this->error = $this->db->lasterror();
  1885. $err++;
  1886. }
  1887. return $err;
  1888. }
  1889. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1890. /**
  1891. * Adds generic parts
  1892. *
  1893. * @return int Error count (0 if OK)
  1894. */
  1895. public function insert_module_parts()
  1896. {
  1897. // phpcs:enable
  1898. global $conf;
  1899. $error = 0;
  1900. if (is_array($this->module_parts) && !empty($this->module_parts)) {
  1901. foreach ($this->module_parts as $key => $value) {
  1902. if (is_array($value) && count($value) == 0) {
  1903. continue; // Discard empty arrays
  1904. }
  1905. $entity = $conf->entity; // Reset the current entity
  1906. $newvalue = $value;
  1907. // Serialize array parameters
  1908. if (is_array($value)) {
  1909. // Can defined other parameters
  1910. // Example when $key='hooks', then $value is an array('data'=>array('hookcontext1','hookcontext2'), 'entity'=>X)
  1911. if (isset($value['data']) && is_array($value['data'])) {
  1912. $newvalue = json_encode($value['data']);
  1913. if (isset($value['entity'])) {
  1914. $entity = $value['entity'];
  1915. }
  1916. } elseif (isset($value['data']) && !is_array($value['data'])) {
  1917. $newvalue = $value['data'];
  1918. if (isset($value['entity'])) {
  1919. $entity = $value['entity'];
  1920. }
  1921. } else { // when hook is declared with syntax 'hook'=>array('hookcontext1','hookcontext2',...)
  1922. $newvalue = json_encode($value);
  1923. }
  1924. }
  1925. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
  1926. $sql .= "name";
  1927. $sql .= ", type";
  1928. $sql .= ", value";
  1929. $sql .= ", note";
  1930. $sql .= ", visible";
  1931. $sql .= ", entity";
  1932. $sql .= ")";
  1933. $sql .= " VALUES (";
  1934. $sql .= " ".$this->db->encrypt($this->const_name."_".strtoupper($key));
  1935. $sql .= ", 'chaine'";
  1936. $sql .= ", ".$this->db->encrypt($newvalue);
  1937. $sql .= ", null";
  1938. $sql .= ", '0'";
  1939. $sql .= ", ".((int) $entity);
  1940. $sql .= ")";
  1941. dol_syslog(get_class($this)."::insert_module_parts for key=".$this->const_name."_".strtoupper($key), LOG_DEBUG);
  1942. $resql = $this->db->query($sql, 1);
  1943. if (!$resql) {
  1944. if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  1945. $error++;
  1946. $this->error = $this->db->lasterror();
  1947. } else {
  1948. dol_syslog(get_class($this)."::insert_module_parts for ".$this->const_name."_".strtoupper($key)." Record already exists.", LOG_WARNING);
  1949. }
  1950. }
  1951. }
  1952. }
  1953. return $error;
  1954. }
  1955. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1956. /**
  1957. * Removes generic parts
  1958. *
  1959. * @return int Error count (0 if OK)
  1960. */
  1961. public function delete_module_parts()
  1962. {
  1963. // phpcs:enable
  1964. global $conf;
  1965. $err = 0;
  1966. $entity = $conf->entity;
  1967. if (is_array($this->module_parts) && !empty($this->module_parts)) {
  1968. dol_syslog(get_class($this)."::delete_module_parts", LOG_DEBUG);
  1969. foreach ($this->module_parts as $key => $value) {
  1970. // If entity is defined
  1971. if (is_array($value) && isset($value['entity'])) {
  1972. $entity = $value['entity'];
  1973. }
  1974. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1975. $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_".strtoupper($key)."'";
  1976. $sql .= " AND entity = ".((int) $entity);
  1977. if (!$this->db->query($sql)) {
  1978. $this->error = $this->db->lasterror();
  1979. $err++;
  1980. }
  1981. }
  1982. }
  1983. return $err;
  1984. }
  1985. /**
  1986. * Function called when module is enabled.
  1987. * The init function adds tabs, constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  1988. * It also creates data directories
  1989. *
  1990. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes', 'menuonly')
  1991. * 'noboxes' = Do not insert boxes 'newboxdefonly' = For boxes, insert def of boxes only and not boxes activation
  1992. * @return int 1 if OK, 0 if KO
  1993. */
  1994. public function init($options = '')
  1995. {
  1996. return $this->_init(array(), $options);
  1997. }
  1998. /**
  1999. * Function called when module is disabled.
  2000. * The remove function removes tabs, constants, boxes, permissions and menus from Dolibarr database.
  2001. * Data directories are not deleted
  2002. *
  2003. * @param string $options Options when enabling module ('', 'noboxes')
  2004. * @return int 1 if OK, 0 if KO
  2005. */
  2006. public function remove($options = '')
  2007. {
  2008. return $this->_remove(array(), $options);
  2009. }
  2010. /**
  2011. * Return Kanban view of a module
  2012. *
  2013. * @param string $codeenabledisable HTML code for button to enable/disable module
  2014. * @param string $codetoconfig HTML code to go to config page
  2015. * @return string HTML code of Kanban view
  2016. */
  2017. public function getKanbanView($codeenabledisable = '', $codetoconfig = '')
  2018. {
  2019. global $conf, $langs;
  2020. // Define imginfo
  2021. $imginfo = "info";
  2022. if ($this->isCoreOrExternalModule() == 'external') {
  2023. $imginfo = "info_black";
  2024. }
  2025. $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($this)));
  2026. $version = $this->getVersion(0);
  2027. $versiontrans = '';
  2028. if (preg_match('/development/i', $version)) {
  2029. $versiontrans .= 'warning';
  2030. }
  2031. if (preg_match('/experimental/i', $version)) {
  2032. $versiontrans .= 'warning';
  2033. }
  2034. if (preg_match('/deprecated/i', $version)) {
  2035. $versiontrans .= 'warning';
  2036. }
  2037. print '
  2038. <div class="box-flex-item info-box-module'
  2039. .(empty($conf->global->$const_name) ? ' --disabled' : '')
  2040. .($this->isCoreOrExternalModule() == 'external' ? ' --external' : '')
  2041. .($this->needUpdate ? ' --need-update' : '')
  2042. .'">
  2043. <div class="info-box info-box-sm info-box-module">
  2044. <div class="info-box-icon'.(empty($conf->global->$const_name) ? '' : ' info-box-icon-module-enabled'.($versiontrans ? ' info-box-icon-module-warning' : '')).'">';
  2045. $alttext = '';
  2046. //if (is_array($objMod->need_dolibarr_version)) $alttext.=($alttext?' - ':'').'Dolibarr >= '.join('.',$objMod->need_dolibarr_version);
  2047. //if (is_array($objMod->phpmin)) $alttext.=($alttext?' - ':'').'PHP >= '.join('.',$objMod->phpmin);
  2048. if (!empty($this->picto)) {
  2049. if (preg_match('/^\//i', $this->picto)) {
  2050. print img_picto($alttext, $this->picto, 'class="inline-block valignmiddle"', 1);
  2051. } else {
  2052. print img_object($alttext, $this->picto, 'class="inline-block valignmiddle"');
  2053. }
  2054. } else {
  2055. print img_object($alttext, 'generic', 'class="inline-block valignmiddle"');
  2056. }
  2057. if ($this->isCoreOrExternalModule() == 'external' || preg_match('/development|experimental|deprecated/i', $version)) {
  2058. $versionTitle = $langs->trans("Version").' '.$this->getVersion(1);
  2059. if ($this->needUpdate) {
  2060. $versionTitle.= '<br>'.$langs->trans('ModuleUpdateAvailable').' : '.$this->lastVersion;
  2061. }
  2062. print '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >';
  2063. print $this->getVersion(1);
  2064. print '</span>';
  2065. }
  2066. print '</div>
  2067. <div class="info-box-content info-box-text-module'.(empty($conf->global->$const_name) ? '' : ' info-box-module-enabled'.($versiontrans ? ' info-box-content-warning' : '')).'">
  2068. <span class="info-box-title">'.$this->getName().'</span>
  2069. <span class="info-box-desc twolinesmax opacitymedium" title="'.dol_escape_htmltag($this->getDesc()).'">'.nl2br($this->getDesc()).'</span>';
  2070. print '<div class="valignmiddle inline-block info-box-more">';
  2071. //if ($versiontrans) print img_warning($langs->trans("Version").' '.$this->getVersion(1)).' ';
  2072. print '<a class="valignmiddle inline-block" href="javascript:document_preview(\''.DOL_URL_ROOT.'/admin/modulehelp.php?id='.$this->numero.'\',\'text/html\',\''.dol_escape_js($langs->trans("Module")).'\')">'.img_picto(($this->isCoreOrExternalModule() == 'external' ? $langs->trans("ExternalModule").' - ' : '').$langs->trans("ClickToShowDescription"), $imginfo).'</a>';
  2073. print '</div><br>';
  2074. print '<div class="valignmiddle inline-block info-box-actions">';
  2075. print '<div class="valignmiddle inline-block info-box-setup">';
  2076. print $codetoconfig;
  2077. print '</div>';
  2078. print '<div class="valignmiddle inline-block marginleftonly marginrightonly">';
  2079. print $codeenabledisable;
  2080. print '</div>';
  2081. print '</div>';
  2082. print '
  2083. </div><!-- /.info-box-content -->
  2084. </div><!-- /.info-box -->
  2085. </div>';
  2086. }
  2087. /**
  2088. * Check for module update
  2089. * TODO : store results for $this->url_last_version and $this->needUpdate
  2090. * Add a cron task to monitor for updates
  2091. *
  2092. * @return int <0 if Error, 0 == no update needed, >0 if need update
  2093. */
  2094. public function checkForUpdate()
  2095. {
  2096. require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  2097. if (!empty($this->url_last_version)) {
  2098. $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only
  2099. if (isset($lastVersion['content']) && strlen($lastVersion['content']) < 30) {
  2100. // Security warning : be careful with remote data content, the module editor could be hacked (or evil) so limit to a-z A-Z 0-9 _ . -
  2101. $this->lastVersion = preg_replace("/[^a-zA-Z0-9_\.\-]+/", "", $lastVersion['content']);
  2102. if (version_compare($this->lastVersion, $this->version) > 0) {
  2103. $this->needUpdate = true;
  2104. return 1;
  2105. } else {
  2106. $this->needUpdate = false;
  2107. return 0;
  2108. }
  2109. } else {
  2110. return -1;
  2111. }
  2112. }
  2113. return 0;
  2114. }
  2115. }