actions_datapolicy.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseom.com>
  3. * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
  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 <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file datapolicy/class/actions_datapolicy.class.php
  20. * \ingroup datapolicy
  21. * \brief Example hook overload.
  22. */
  23. /**
  24. * Class ActionsDatapolicy
  25. */
  26. class ActionsDatapolicy
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error
  34. */
  35. public $error = '';
  36. /**
  37. * @var array Errors
  38. */
  39. public $errors = array();
  40. /**
  41. * @var array Hook results. Propagated to $hookmanager->resArray for later reuse
  42. */
  43. public $results = array();
  44. /**
  45. * @var string String displayed by executeHook() immediately after return
  46. */
  47. public $resprints;
  48. /**
  49. * Constructor
  50. *
  51. * @param DoliDB $db Database handler
  52. */
  53. public function __construct($db)
  54. {
  55. $this->db = $db;
  56. }
  57. /**
  58. * Execute action
  59. *
  60. * @param array $parameters Array of parameters
  61. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  62. * @param string $action 'add', 'update', 'view'
  63. * @return int <0 if KO,
  64. * =0 if OK but we want to process standard actions too,
  65. * >0 if OK and we want to replace standard actions.
  66. */
  67. public function getNomUrl($parameters, &$object, &$action)
  68. {
  69. global $db, $langs, $conf, $user;
  70. $this->resprints = '';
  71. return 0;
  72. }
  73. /**
  74. * Overloading the doActions function : replacing the parent's function with the one below
  75. *
  76. * @param array $parameters Hook metadatas (context, etc...)
  77. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  78. * @param string $action Current action (if set). Generally create or edit or null
  79. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  80. * @return int < 0 on error, 0 on success, 1 to replace standard code
  81. */
  82. public function doActions($parameters, &$object, &$action, $hookmanager)
  83. {
  84. global $conf, $user, $langs;
  85. $langs->load('datapolicy@datapolicy');
  86. $error = 0; // Error counter
  87. if (GETPOST('socid') && $parameters['currentcontext'] == 'thirdpartycard' && !empty($object)) {
  88. $object->fetch(GETPOST('socid'));
  89. }
  90. // FIXME Removed hard coded id, use codes
  91. if ($parameters['currentcontext'] == 'thirdpartycard' && $action == 'anonymiser' && (in_array($object->forme_juridique_code, array(11, 12, 13, 15, 17, 18, 19, 35, 60, 200, 311, 312, 316, 401, 600, 700, 1005)) || $object->typent_id == 8)) {
  92. // on verifie si l'objet est utilisé
  93. if ($object->isObjectUsed(GETPOST('socid'))) {
  94. $object->name = $langs->trans('ANONYME');
  95. $object->name_bis = '';
  96. $object->name_alias = '';
  97. $object->address = '';
  98. $object->town = '';
  99. $object->zip = '';
  100. $object->phone = '';
  101. $object->email = '';
  102. $object->url = '';
  103. $object->fax = '';
  104. $object->state = '';
  105. $object->country = '';
  106. $object->state_id = '';
  107. $object->skype = '';
  108. $object->country_id = '';
  109. $object->note_private = $object->note_private.'<br>'.$langs->trans('ANONYMISER_AT', dol_print_date(time()));
  110. if ($object->update($object->id, $user, 0)) {
  111. // On supprime les contacts associé
  112. $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = ".((int) $object->id);
  113. $this->db->query($sql);
  114. setEventMessages($langs->trans('ANONYMISER_SUCCESS'), array());
  115. header('Location:'.$_SERVER["PHP_SELF"]."?socid=".$object->id);
  116. }
  117. }
  118. } elseif ($parameters['currentcontext'] == 'thirdpartycard' && $action == 'datapolicy_portabilite') {
  119. header('Content-Type: application/csv');
  120. header('Content-Disposition: attachment; filename=datapolicy_portabilite.csv');
  121. header('Pragma: no-cache');
  122. $object->fetch(GETPOST('socid'));
  123. echo 'Name;Firstname;Civility;Thirdparty;Function;Address;ZipCode;City;Department;Country;Email;Pro Phone;Perso Phone;Mobile Phone;Instant Mail;Birthday;'.PHP_EOL;
  124. echo $object->name.';';
  125. echo ';';
  126. echo ';';
  127. echo ';';
  128. echo ';';
  129. echo $object->address.';';
  130. echo $object->zip.';';
  131. echo $object->town.';';
  132. echo $object->state.';';
  133. echo $object->country.';';
  134. echo $object->email.';';
  135. echo $object->phone.';';
  136. echo ';';
  137. echo ';';
  138. echo $object->skype.';';
  139. echo ';';
  140. exit;
  141. } elseif ($parameters['currentcontext'] == 'membercard' && $action == 'datapolicy_portabilite') {
  142. header('Content-Type: application/csv');
  143. header('Content-Disposition: attachment; filename=datapolicy_portabilite.csv');
  144. header('Pragma: no-cache');
  145. $soc = $object->fetch_thirdparty();
  146. echo 'Name;Firstname;Civility;Thirdparty;Function;Address;ZipCode;City;Department;Country;Email;Pro Phone;Perso Phone;Mobile Phone;Instant Mail;Birthday;'.PHP_EOL;
  147. echo $object->lastname.';';
  148. echo $object->firstname.';';
  149. echo $object->getCivilityLabel().';';
  150. echo ($soc != -1 ? $object->thirdparty->name : '').';';
  151. echo ';';
  152. echo $object->address.';';
  153. echo $object->zip.';';
  154. echo $object->town.';';
  155. echo $object->state.';';
  156. echo $object->country.';';
  157. echo $object->email.';';
  158. echo $object->phone.';';
  159. echo $object->phone_perso.';';
  160. echo $object->phone_mobile.';';
  161. echo $object->skype.';';
  162. echo dol_print_date($object->birth).';';
  163. exit;
  164. } elseif ($parameters['currentcontext'] == 'contactcard' && $action == 'datapolicy_portabilite') {
  165. $object->fetch(GETPOST('id'));
  166. header('Content-Type: application/csv');
  167. header('Content-Disposition: attachment; filename=datapolicy_portabilite.csv');
  168. header('Pragma: no-cache');
  169. $soc = $object->fetch_thirdparty();
  170. echo 'Name;Firstname;Civility;Thirdparty;Function;Address;ZipCode;City;Department;Country;Email;Pro Phone;Perso Phone;Mobile Phone;Instant Mail;Birthday;'.PHP_EOL;
  171. echo $object->lastname.';';
  172. echo $object->firstname.';';
  173. echo $object->getCivilityLabel().';';
  174. echo ($soc != -1 ? $object->thirdparty->name : '').';';
  175. echo $object->poste.';';
  176. echo $object->address.';';
  177. echo $object->zip.';';
  178. echo $object->town.';';
  179. echo $object->state.';';
  180. echo $object->country.';';
  181. echo $object->email.';';
  182. echo $object->phone_pro.';';
  183. echo $object->phone_perso.';';
  184. echo $object->phone_mobile.';';
  185. echo dol_print_date($object->birth).';';
  186. exit;
  187. } elseif ($parameters['currentcontext'] == 'contactcard' && $action == 'send_datapolicy') {
  188. $object->fetch(GETPOST('id'));
  189. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  190. require_once DOL_DOCUMENT_ROOT.'/datapolicy/class/datapolicy.class.php';
  191. DataPolicy::sendMailDataPolicyContact($object);
  192. } elseif ($parameters['currentcontext'] == 'membercard' && $action == 'send_datapolicy') {
  193. $object->fetch(GETPOST('id'));
  194. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  195. require_once DOL_DOCUMENT_ROOT.'/datapolicy/class/datapolicy.class.php';
  196. DataPolicy::sendMailDataPolicyAdherent($object);
  197. } elseif ($parameters['currentcontext'] == 'thirdpartycard' && $action == 'send_datapolicy') {
  198. $object->fetch(GETPOST('socid'));
  199. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  200. require_once DOL_DOCUMENT_ROOT.'/datapolicy/class/datapolicy.class.php';
  201. DataPolicy::sendMailDataPolicyCompany($object);
  202. }
  203. if (!$error) {
  204. $this->results = array('myreturn' => 999);
  205. $this->resprints = 'A text to show';
  206. return 0; // or return 1 to replace standard code
  207. } else {
  208. $this->errors[] = 'Error message';
  209. return -1;
  210. }
  211. }
  212. /**
  213. * Overloading the doActions function : replacing the parent's function with the one below
  214. *
  215. * @param array $parameters Hook metadatas (context, etc...)
  216. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  217. * @param string $action Current action (if set). Generally create or edit or null
  218. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  219. * @return int < 0 on error, 0 on success, 1 to replace standard code
  220. */
  221. public function doMassActions($parameters, &$object, &$action, $hookmanager)
  222. {
  223. global $conf, $user, $langs;
  224. $error = 0; // Error counter
  225. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  226. //if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
  227. // // do something only for the context 'somecontext1' or 'somecontext2'
  228. // foreach ($parameters['toselect'] as $objectid) {
  229. // // Do action on each object id
  230. // }
  231. //}
  232. if (!$error) {
  233. $this->results = array('myreturn' => 999);
  234. $this->resprints = 'A text to show';
  235. return 0; // or return 1 to replace standard code
  236. } else {
  237. $this->errors[] = 'Error message';
  238. return -1;
  239. }
  240. }
  241. /**
  242. * Overloading the addMoreMassActions function : replacing the parent's function with the one below
  243. *
  244. * @param array $parameters Hook metadatas (context, etc...)
  245. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  246. * @param string $action Current action (if set). Generally create or edit or null
  247. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  248. * @return int < 0 on error, 0 on success, 1 to replace standard code
  249. */
  250. public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
  251. {
  252. global $conf, $user, $langs;
  253. $error = 0; // Error counter
  254. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  255. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  256. $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("datapolicyMassAction").'</option>';
  257. }
  258. if (!$error) {
  259. return 0; // or return 1 to replace standard code
  260. } else {
  261. $this->errors[] = 'Error message';
  262. return -1;
  263. }
  264. }
  265. /**
  266. * Execute action
  267. *
  268. * @param array $parameters Array of parameters
  269. * @param Object $object Object output on PDF
  270. * @param string $action 'add', 'update', 'view'
  271. * @return int <0 if KO,
  272. * =0 if OK but we want to process standard actions too,
  273. * >0 if OK and we want to replace standard actions.
  274. */
  275. public function beforePDFCreation($parameters, &$object, &$action)
  276. {
  277. global $conf, $user, $langs;
  278. global $hookmanager;
  279. $outputlangs = $langs;
  280. $ret = 0;
  281. $deltemp = array();
  282. dol_syslog(get_class($this).'::executeHooks action='.$action);
  283. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  284. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  285. }
  286. return $ret;
  287. }
  288. /**
  289. * addMoreActionsButtons
  290. *
  291. * @param array $parameters array of parameters
  292. * @param Object $object Object
  293. * @param string $action Actions
  294. * @param HookManager $hookmanager Hook manager
  295. * @return void
  296. */
  297. public function addMoreActionsButtons($parameters, &$object, &$action, $hookmanager)
  298. {
  299. global $conf, $user, $langs;
  300. $langs->load('datapolicy@datapolicy');
  301. if (!empty($conf->global->DATAPOLICY_ENABLE_EMAILS)) {
  302. $dialog = '<div id="dialogdatapolicy" style="display:none;" title="'.$langs->trans('DATAPOLICY_PORTABILITE_TITLE').'">';
  303. $dialog .= '<div class="confirmmessage">'.img_help('', '').' '.$langs->trans('DATAPOLICY_PORTABILITE_CONFIRMATION').'</div>';
  304. $dialog .= "</div>";
  305. $dialog .= '<script>
  306. $( function() {
  307. $("#rpgpdbtn").on("click", function(){
  308. var href = $(this).attr("href");
  309. $( "#dialogdatapolicy" ).dialog({
  310. modal: true,
  311. buttons: {
  312. "OK": function() {
  313. window.open(href);
  314. $( this ).dialog( "close" );
  315. },
  316. "' . $langs->trans("Cancel").'": function() {
  317. $( this ).dialog( "close" );
  318. }
  319. }
  320. });
  321. return false;
  322. });
  323. } );
  324. </script>';
  325. echo $dialog;
  326. if ($parameters['currentcontext'] == 'thirdpartycard' && in_array($object->forme_juridique_code, array(11, 12, 13, 15, 17, 18, 19, 35, 60, 200, 311, 312, 316, 401, 600, 700, 1005)) || $object->typent_id == 8) {
  327. echo '<div class="inline-block divButAction"><a target="_blank" rel="noopener noreferrer" id="rpgpdbtn" class="butAction" href="'.$_SERVER["PHP_SELF"]."?socid=".$object->id.'&action=datapolicy_portabilite" title="'.$langs->trans('DATAPOLICY_PORTABILITE_TITLE').'">'.$langs->trans("DATAPOLICY_PORTABILITE").'</a></div>';
  328. } elseif ($parameters['currentcontext'] == 'membercard') {
  329. echo '<div class="inline-block divButAction"><a target="_blank" rel="noopener noreferrer" id="rpgpdbtn" class="butAction" href="'.$_SERVER["PHP_SELF"]."?rowid=".$object->id.'&action=datapolicy_portabilite" title="'.$langs->trans('DATAPOLICY_PORTABILITE_TITLE').'">'.$langs->trans("DATAPOLICY_PORTABILITE").'</a></div>';
  330. } elseif ($parameters['currentcontext'] == 'contactcard') {
  331. echo '<div class="inline-block divButAction"><a target="_blank" rel="noopener noreferrer" id="rpgpdbtn" class="butAction" href="'.$_SERVER["PHP_SELF"]."?id=".$object->id.'&action=datapolicy_portabilite" title="'.$langs->trans('DATAPOLICY_PORTABILITE_TITLE').'">'.$langs->trans("DATAPOLICY_PORTABILITE").'</a></div>';
  332. }
  333. if (!empty($object->mail) && empty($object->array_options['options_datapolicy_send']) && $parameters['currentcontext'] == 'thirdpartycard' && in_array($object->forme_juridique_code, array(11, 12, 13, 15, 17, 18, 19, 35, 60, 200, 311, 312, 316, 401, 600, 700, 1005)) || $object->typent_id == 8) {
  334. echo '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"]."?socid=".$object->id.'&action=send_datapolicy" title="'.$langs->trans('DATAPOLICY_SEND').'">'.$langs->trans("DATAPOLICY_SEND").'</a></div>';
  335. } elseif (!empty($object->mail) && empty($object->array_options['options_datapolicy_send']) && $parameters['currentcontext'] == 'membercard') {
  336. echo '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"]."?rowid=".$object->id.'&action=send_datapolicy" title="'.$langs->trans('DATAPOLICY_SEND').'">'.$langs->trans("DATAPOLICY_SEND").'</a></div>';
  337. } elseif (!empty($object->mail) && empty($object->array_options['options_datapolicy_send']) && $parameters['currentcontext'] == 'contactcard') {
  338. echo '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"]."?id=".$object->id.'&action=send_datapolicy" title="'.$langs->trans('DATAPOLICY_SEND').'">'.$langs->trans("DATAPOLICY_SEND").'</a></div>';
  339. }
  340. }
  341. }
  342. /**
  343. * printCommonFooter
  344. *
  345. * @param array $parameters array of parameters
  346. * @param Object $object Object
  347. * @param string $action Actions
  348. * @param HookManager $hookmanager Hook manager
  349. * @return int
  350. */
  351. public function printCommonFooter($parameters, &$object, &$action, $hookmanager)
  352. {
  353. global $conf, $user, $langs;
  354. $jsscript = '';
  355. if ($parameters['currentcontext'] == 'thirdpartycard') {
  356. if (GETPOST('action') == 'create' || GETPOST('action') == 'edit' || GETPOST('action') == '') {
  357. $jsscript .= '<script>';
  358. $jsscript .= "var elementToHide = 'tr.societe_extras_datapolicy_consentement, tr.societe_extras_datapolicy_opposition_traitement, tr.societe_extras_datapolicy_opposition_prospection';".PHP_EOL;
  359. $jsscript .= "var forme_juridique = [".PHP_EOL;
  360. $jsscript .= "11, 12, 13, 15, 17, 18, 19, 35, 60, 200, 311, 312, 316, 401, 600, 700, 1005".PHP_EOL;
  361. $jsscript .= "];".PHP_EOL;
  362. $jsscript .= "function hideRgPD() {".PHP_EOL;
  363. $jsscript .= " if ($('#typent_id').val() == 8 || forme_juridique.indexOf(parseInt($('#forme_juridique_code').val())) > -1) {".PHP_EOL;
  364. $jsscript .= " console.log(elementToHide);".PHP_EOL;
  365. $jsscript .= " $('tr.societe_extras_datapolicy_consentement, tr.societe_extras_datapolicy_opposition_traitement, tr.societe_extras_datapolicy_opposition_prospection').show(); } else { $('tr.societe_extras_datapolicy_consentement, tr.societe_extras_datapolicy_opposition_traitement, tr.societe_extras_datapolicy_opposition_prospection').hide(); }}".PHP_EOL;
  366. $jsscript .= "hideRgPD();".PHP_EOL;
  367. $jsscript .= "$('#forme_juridique_code, #typent_id').change(function(){ hideRgPD(); });".PHP_EOL;
  368. $jsscript .= '</script>';
  369. } elseif (GETPOST('action') == 'confirm_delete' && GETPOST('confirm') == 'yes' && GETPOST('socid') > 0) {
  370. // La suppression n'a pas été possible
  371. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  372. $societe = new Societe($this->db);
  373. $societe->fetch(GETPOST('socid'));
  374. // On vérifie si il est utilisé
  375. if ((in_array($object->forme_juridique_code, array(11, 12, 13, 15, 17, 18, 19, 35, 60, 200, 311, 312, 316, 401, 600, 700, 1005)) || $societe->typent_id == 8) && $societe->isObjectUsed(GETPOST('socid'))) {
  376. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  377. $form = new Form($this->db);
  378. echo $form->formconfirm($_SERVER["PHP_SELF"]."?socid=".GETPOST('socid'), substr($langs->trans("DATAPOLICY_POPUP_ANONYME_TITLE"), 0, strlen($langs->trans("DATAPOLICY_POPUP_ANONYME_TITLE")) - 2), $langs->trans("DATAPOLICY_POPUP_ANONYME_TEXTE"), 'anonymiser', '', '', 1);
  379. }
  380. }
  381. if (GETPOST('socid')) {
  382. /* Removed due to awful harcoded values
  383. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  384. $societe = new Societe($this->db);
  385. $societe->fetch(GETPOST('socid'));
  386. if (!empty($object->forme_juridique_code) && !in_array($object->forme_juridique_code, array(11, 12, 13, 15, 17, 18, 19, 35, 60, 200, 311, 312, 316, 401, 600, 700, 1005)) && $societe->typent_id != 8) {
  387. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  388. $jsscript .= '<script>';
  389. $jsscript .= "var elementToHide = 'td.societe_extras_datapolicy_opposition_traitement, td.societe_extras_datapolicy_opposition_prospection, td.societe_extras_datapolicy_consentement';".PHP_EOL;
  390. $jsscript .= "$(elementToHide).parent('tr').hide();".PHP_EOL;
  391. $jsscript .= '</script>';
  392. }
  393. */
  394. }
  395. } elseif ($parameters['currentcontext'] == 'contactcard') {
  396. if (GETPOST('action') == 'create' || GETPOST('action') == 'edit') {
  397. $jsscript .= '<script>';
  398. $jsscript .= "$('#options_datapolicy_opposition_traitement, #options_datapolicy_opposition_prospection, input[name=\"options_datapolicy_opposition_traitement\"], input[name=\"options_datapolicy_opposition_prospection\"]').change(function(){
  399. if($('#options_datapolicy_opposition_traitement').prop('checked') == true || $('input[name=options_datapolicy_opposition_traitement]').prop('checked') || $('#options_datapolicy_opposition_prospection').prop('checked') || $('input[name=options_datapolicy_opposition_prospection]').prop('checked')) {
  400. $('#no_email').val(1);
  401. }
  402. });";
  403. $jsscript .= '</script>';
  404. }
  405. }
  406. $this->resprint = $jsscript;
  407. return 0;
  408. }
  409. }