contract.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. /* Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
  3. * Copyright (C) 2011-2015 Philippe Grand <philippe.grand@atoo-net.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/admin/contract.php
  20. * \ingroup contract
  21. * \brief Setup page of module Contracts
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/contract.lib.php';
  27. $langs->load("admin");
  28. $langs->load("errors");
  29. $langs->load("contracts");
  30. if (!$user->admin) accessforbidden();
  31. $action = GETPOST('action','alpha');
  32. $value = GETPOST('value','alpha');
  33. $label = GETPOST('label','alpha');
  34. $scandir = GETPOST('scandir','alpha');
  35. $type='contract';
  36. if (empty($conf->global->CONTRACT_ADDON))
  37. {
  38. $conf->global->CONTRACT_ADDON='mod_contract_serpis';
  39. }
  40. /*
  41. * Actions
  42. */
  43. if ($action == 'updateMask')
  44. {
  45. $maskconst = GETPOST('maskconstcontract','alpha');
  46. $maskvalue = GETPOST('maskcontract','alpha');
  47. if ($maskconst) $res = dolibarr_set_const($db,$maskconst,$maskvalue,'chaine',0,'',$conf->entity);
  48. if (! $res > 0) $error++;
  49. if (! $error)
  50. {
  51. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  52. }
  53. else
  54. {
  55. setEventMessages($langs->trans("Error"), null, 'errors');
  56. }
  57. }
  58. else if ($action == 'specimen') // For contract
  59. {
  60. $modele= GETPOST('module','alpha');
  61. $contract = new Contrat($db);
  62. $contract->initAsSpecimen();
  63. // Search template files
  64. $file=''; $classname=''; $filefound=0;
  65. $dirmodels=array_merge(array('/'),(array) $conf->modules_parts['models']);
  66. foreach($dirmodels as $reldir)
  67. {
  68. $file=dol_buildpath($reldir."core/modules/contract/doc/pdf_".$modele.".modules.php",0);
  69. if (file_exists($file))
  70. {
  71. $filefound=1;
  72. $classname = "pdf_".$modele;
  73. break;
  74. }
  75. }
  76. if ($filefound)
  77. {
  78. require_once $file;
  79. $module = new $classname($db);
  80. if ($module->write_file($contract,$langs) > 0)
  81. {
  82. header("Location: ".DOL_URL_ROOT."/document.php?modulepart=contract&file=SPECIMEN.pdf");
  83. return;
  84. }
  85. else
  86. {
  87. setEventMessages($obj->error, $obj->errors, 'errors');
  88. dol_syslog($obj->error, LOG_ERR);
  89. }
  90. }
  91. else
  92. {
  93. setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
  94. dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
  95. }
  96. }
  97. // Define constants for submodules that contains parameters (forms with param1, param2, ... and value1, value2, ...)
  98. if ($action == 'setModuleOptions')
  99. {
  100. $post_size=count($_POST);
  101. $db->begin();
  102. for($i=0;$i < $post_size;$i++)
  103. {
  104. if (array_key_exists('param'.$i,$_POST))
  105. {
  106. $param=GETPOST("param".$i,'alpha');
  107. $value=GETPOST("value".$i,'alpha');
  108. if ($param) $res = dolibarr_set_const($db,$param,$value,'chaine',0,'',$conf->entity);
  109. if (! $res > 0) $error++;
  110. }
  111. }
  112. if (! $error)
  113. {
  114. $db->commit();
  115. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  116. }
  117. else
  118. {
  119. $db->rollback();
  120. setEventMessages($langs->trans("Error"), null, 'errors');
  121. }
  122. }
  123. // Activate a model
  124. else if ($action == 'set')
  125. {
  126. $ret = addDocumentModel($value, $type, $label, $scandir);
  127. }
  128. else if ($action == 'del')
  129. {
  130. $ret = delDocumentModel($value, $type);
  131. if ($ret > 0)
  132. {
  133. if ($conf->global->CONTRACT_ADDON_PDF == "$value") dolibarr_del_const($db, 'CONTRACT_ADDON_PDF',$conf->entity);
  134. }
  135. }
  136. // Set default model
  137. else if ($action == 'setdoc')
  138. {
  139. if (dolibarr_set_const($db, "CONTRACT_ADDON_PDF",$value,'chaine',0,'',$conf->entity))
  140. {
  141. // La constante qui a ete lue en avant du nouveau set
  142. // on passe donc par une variable pour avoir un affichage coherent
  143. $conf->global->CONTRACT_ADDON_PDF = $value;
  144. }
  145. // On active le modele
  146. $ret = delDocumentModel($value, $type);
  147. if ($ret > 0)
  148. {
  149. $ret = addDocumentModel($value, $type, $label, $scandir);
  150. }
  151. }
  152. else if ($action == 'setmod')
  153. {
  154. // TODO Verifier si module numerotation choisi peut etre active
  155. // par appel methode canBeActivated
  156. dolibarr_set_const($db, "CONTRACT_ADDON",$value,'chaine',0,'',$conf->entity);
  157. }
  158. else if ($action == 'set_other')
  159. {
  160. $freetext= GETPOST('CONTRACT_FREE_TEXT'); // No alpha here, we want exact string
  161. $res1 = dolibarr_set_const($db, "CONTRACT_FREE_TEXT",$freetext,'chaine',0,'',$conf->entity);
  162. $draft= GETPOST('CONTRACT_DRAFT_WATERMARK','alpha');
  163. $res2 = dolibarr_set_const($db, "CONTRACT_DRAFT_WATERMARK",trim($draft),'chaine',0,'',$conf->entity);
  164. if (! $res1 > 0 || ! $res2 > 0) $error++;
  165. if (! $error)
  166. {
  167. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  168. }
  169. else
  170. {
  171. setEventMessages($langs->trans("Error"), null, 'errors');
  172. }
  173. }
  174. /*
  175. * View
  176. */
  177. $dirmodels=array_merge(array('/'),(array) $conf->modules_parts['models']);
  178. llxHeader();
  179. $form=new Form($db);
  180. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
  181. print load_fiche_titre($langs->trans("ContractsSetup"),$linkback,'title_setup');
  182. $head=contract_admin_prepare_head();
  183. dol_fiche_head($head, 'contract', $langs->trans("Contracts"), -1, 'contract');
  184. /*
  185. * Contracts Numbering model
  186. */
  187. print load_fiche_titre($langs->trans("ContractsNumberingModules"),'','');
  188. print '<table class="noborder" width="100%">';
  189. print '<tr class="liste_titre">';
  190. print '<td width="100">'.$langs->trans("Name").'</td>';
  191. print '<td>'.$langs->trans("Description").'</td>';
  192. print '<td>'.$langs->trans("Example").'</td>';
  193. print '<td align="center" width="60">'.$langs->trans("Status").'</td>';
  194. print '<td align="center" width="16">'.$langs->trans("ShortInfo").'</td>';
  195. print "</tr>\n";
  196. clearstatcache();
  197. foreach ($dirmodels as $reldir)
  198. {
  199. $dir = dol_buildpath($reldir."core/modules/contract/");
  200. if (is_dir($dir))
  201. {
  202. $handle = opendir($dir);
  203. if (is_resource($handle))
  204. {
  205. $var=true;
  206. while (($file = readdir($handle))!==false)
  207. {
  208. if (substr($file, 0, 13) == 'mod_contract_' && substr($file, dol_strlen($file)-3, 3) == 'php')
  209. {
  210. $file = substr($file, 0, dol_strlen($file)-4);
  211. require_once $dir.$file.'.php';
  212. $module = new $file($db);
  213. // Show modules according to features level
  214. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
  215. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
  216. if ($module->isEnabled())
  217. {
  218. print '<tr class="oddeven"><td>'.$module->nom."</td><td>\n";
  219. print $module->info();
  220. print '</td>';
  221. // Show example of numbering model
  222. print '<td class="nowrap">';
  223. $tmp=$module->getExample();
  224. if (preg_match('/^Error/',$tmp)) { $langs->load("errors"); print '<div class="error">'.$langs->trans($tmp).'</div>'; }
  225. elseif ($tmp=='NotConfigured') print $langs->trans($tmp);
  226. else print $tmp;
  227. print '</td>'."\n";
  228. print '<td align="center">';
  229. if ($conf->global->CONTRACT_ADDON == "$file")
  230. {
  231. print img_picto($langs->trans("Activated"),'switch_on');
  232. }
  233. else
  234. {
  235. print '<a href="'.$_SERVER["PHP_SELF"].'?action=setmod&amp;value='.$file.'">';
  236. print img_picto($langs->trans("Disabled"),'switch_off');
  237. print '</a>';
  238. }
  239. print '</td>';
  240. $contract=new Contrat($db);
  241. $contract->initAsSpecimen();
  242. // Info
  243. $htmltooltip='';
  244. $htmltooltip.=''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  245. $nextval=$module->getNextValue($mysoc,$contract);
  246. if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
  247. $htmltooltip.=''.$langs->trans("NextValue").': ';
  248. if ($nextval) {
  249. if (preg_match('/^Error/',$nextval) || $nextval=='NotConfigured')
  250. $nextval = $langs->trans($nextval);
  251. $htmltooltip.=$nextval.'<br>';
  252. } else {
  253. $htmltooltip.=$langs->trans($module->error).'<br>';
  254. }
  255. }
  256. print '<td align="center">';
  257. print $form->textwithpicto('',$htmltooltip,1,0);
  258. print '</td>';
  259. print '</tr>';
  260. }
  261. }
  262. }
  263. closedir($handle);
  264. }
  265. }
  266. }
  267. print '</table><br>';
  268. /*
  269. * Documents models for Contracts
  270. */
  271. print load_fiche_titre($langs->trans("TemplatePDFContracts"),'','');
  272. // Defini tableau def des modeles
  273. $def = array();
  274. $sql = "SELECT nom";
  275. $sql.= " FROM ".MAIN_DB_PREFIX."document_model";
  276. $sql.= " WHERE type = '".$type."'";
  277. $sql.= " AND entity = ".$conf->entity;
  278. $resql=$db->query($sql);
  279. if ($resql)
  280. {
  281. $i = 0;
  282. $num_rows=$db->num_rows($resql);
  283. while ($i < $num_rows)
  284. {
  285. $array = $db->fetch_array($resql);
  286. array_push($def, $array[0]);
  287. $i++;
  288. }
  289. }
  290. else
  291. {
  292. dol_print_error($db);
  293. }
  294. print '<table class="noborder" width="100%">';
  295. print '<tr class="liste_titre">';
  296. print '<td>'.$langs->trans("Name").'</td>';
  297. print '<td>'.$langs->trans("Description").'</td>';
  298. print '<td align="center" width="60">'.$langs->trans("Status")."</td>\n";
  299. print '<td align="center" width="60">'.$langs->trans("Default")."</td>\n";
  300. print '<td align="center" width="80">'.$langs->trans("ShortInfo").'</td>';
  301. print '<td align="center" width="80">'.$langs->trans("Preview").'</td>';
  302. print "</tr>\n";
  303. clearstatcache();
  304. $var=true;
  305. foreach ($dirmodels as $reldir)
  306. {
  307. foreach (array('','/doc') as $valdir)
  308. {
  309. $dir = dol_buildpath($reldir."core/modules/contract".$valdir);
  310. if (is_dir($dir))
  311. {
  312. $handle=opendir($dir);
  313. if (is_resource($handle))
  314. {
  315. while (($file = readdir($handle))!==false)
  316. {
  317. $filelist[]=$file;
  318. }
  319. closedir($handle);
  320. arsort($filelist);
  321. foreach($filelist as $file)
  322. {
  323. if (preg_match('/\.modules\.php$/i',$file) && preg_match('/^(pdf_|doc_)/',$file))
  324. {
  325. if (file_exists($dir.'/'.$file))
  326. {
  327. $name = substr($file, 4, dol_strlen($file) -16);
  328. $classname = substr($file, 0, dol_strlen($file) -12);
  329. require_once $dir.'/'.$file;
  330. $module = new $classname($db);
  331. $modulequalified=1;
  332. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) $modulequalified=0;
  333. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) $modulequalified=0;
  334. if ($modulequalified)
  335. {
  336. $var = !$var;
  337. print '<tr class="oddeven"><td width="100">';
  338. print (empty($module->name)?$name:$module->name);
  339. print "</td><td>\n";
  340. if (method_exists($module,'info')) print $module->info($langs);
  341. else print $module->description;
  342. print '</td>';
  343. // Active
  344. if (in_array($name, $def))
  345. {
  346. print '<td align="center">'."\n";
  347. print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&value='.$name.'">';
  348. print img_picto($langs->trans("Enabled"),'switch_on');
  349. print '</a>';
  350. print '</td>';
  351. }
  352. else
  353. {
  354. print '<td align="center">'."\n";
  355. print '<a href="'.$_SERVER["PHP_SELF"].'?action=set&value='.$name.'&amp;scandir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"),'switch_off').'</a>';
  356. print "</td>";
  357. }
  358. // Defaut
  359. print '<td align="center">';
  360. if ($conf->global->CONTRACT_ADDON_PDF == $name)
  361. {
  362. print img_picto($langs->trans("Default"),'on');
  363. }
  364. else
  365. {
  366. print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&value='.$name.'&amp;scandir='.$module->scandir.'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"),'off').'</a>';
  367. }
  368. print '</td>';
  369. // Info
  370. $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
  371. $htmltooltip.='<br>'.$langs->trans("Type").': '.($module->type?$module->type:$langs->trans("Unknown"));
  372. if ($module->type == 'pdf')
  373. {
  374. $htmltooltip.='<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
  375. }
  376. $htmltooltip.='<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
  377. $htmltooltip.='<br>'.$langs->trans("Logo").': '.yn($module->option_logo,1,1);
  378. $htmltooltip.='<br>'.$langs->trans("PaymentMode").': '.yn($module->option_modereg,1,1);
  379. $htmltooltip.='<br>'.$langs->trans("PaymentConditions").': '.yn($module->option_condreg,1,1);
  380. $htmltooltip.='<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang,1,1);
  381. $htmltooltip.='<br>'.$langs->trans("WatermarkOnDraftOrders").': '.yn($module->option_draft_watermark,1,1);
  382. print '<td align="center">';
  383. print $form->textwithpicto('',$htmltooltip,1,0);
  384. print '</td>';
  385. // Preview
  386. print '<td align="center">';
  387. if ($module->type == 'pdf')
  388. {
  389. print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"),'contract').'</a>';
  390. }
  391. else
  392. {
  393. print img_object($langs->trans("PreviewNotAvailable"),'generic');
  394. }
  395. print '</td>';
  396. print "</tr>\n";
  397. }
  398. }
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }
  405. print '</table>';
  406. print "<br>";
  407. /*
  408. * Other options
  409. *
  410. */
  411. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  412. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  413. print '<input type="hidden" name="action" value="set_other">';
  414. print load_fiche_titre($langs->trans("OtherOptions"),'','');
  415. print '<table class="noborder" width="100%">';
  416. print '<tr class="liste_titre">';
  417. print '<td>'.$langs->trans("Parameter").'</td>';
  418. print '<td align="center" width="60">'.$langs->trans("Value").'</td>';
  419. print "</tr>\n";
  420. $var=true;
  421. $substitutionarray=pdf_getSubstitutionArray($langs);
  422. $substitutionarray['__(AnyTranslationKey)__']=$langs->trans("Translation");
  423. $htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
  424. foreach($substitutionarray as $key => $val) $htmltext.=$key.'<br>';
  425. $htmltext.='</i>';
  426. $var=! $var;
  427. print '<tr class="oddeven"><td colspan="2">';
  428. //print $form->textwithpicto($langs->trans("FreeLegalTextOnContracts"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'tooltiphelp');
  429. print $form->textwithpicto($langs->trans("FreeLegalTextOnContracts"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2);
  430. print '<br>';
  431. $variablename='CONTRACT_FREE_TEXT';
  432. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT))
  433. {
  434. print '<textarea name="'.$variablename.'" class="flat" cols="120">'.$conf->global->$variablename.'</textarea>';
  435. }
  436. else
  437. {
  438. include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  439. $doleditor=new DolEditor($variablename, $conf->global->$variablename,'',80,'dolibarr_details');
  440. print $doleditor->Create();
  441. }
  442. print '</td></tr>'."\n";
  443. //Use draft Watermark
  444. print '<tr class="oddeven"><td>';
  445. //print $form->textwithpicto($langs->trans("WatermarkOnDraftContractCards"), $htmltext, 1, 'help', '', 0, 2, 'tooltiphelp');
  446. print $form->textwithpicto($langs->trans("WatermarkOnDraftContractCards"), $htmltext, 1, 'help', '', 0, 2);
  447. print '</td><td>';
  448. print '<input size="50" class="flat" type="text" name="CONTRACT_DRAFT_WATERMARK" value="'.$conf->global->CONTRACT_DRAFT_WATERMARK.'">';
  449. print '</td></tr>'."\n";
  450. print '</table>';
  451. print '<div class="center">';
  452. print '<input type="submit" class="button" value="'.$langs->trans("Save").'">';
  453. print '</div>';
  454. print '</form>';
  455. dol_fiche_end();
  456. llxFooter();
  457. $db->close();