actions_datapolicy.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 Societe|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_alias = '';
  96. $object->address = '';
  97. $object->town = '';
  98. $object->zip = '';
  99. $object->phone = '';
  100. $object->email = '';
  101. $object->url = '';
  102. $object->fax = '';
  103. $object->state = '';
  104. $object->country = '';
  105. $object->state_id = '';
  106. $object->socialnetworks = '';
  107. $object->country_id = '';
  108. $object->note_private = dol_concatdesc($object->note_private, $langs->trans('ANONYMISER_AT', dol_print_date(dol_now())));
  109. if ($object->update($object->id, $user, 0)) {
  110. // On supprime les contacts associé
  111. $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = ".((int) $object->id);
  112. $this->db->query($sql);
  113. setEventMessages($langs->trans('ANONYMISER_SUCCESS'), array());
  114. header('Location:'.$_SERVER["PHP_SELF"]."?socid=".$object->id);
  115. }
  116. }
  117. } elseif ($parameters['currentcontext'] == 'contactcard' && $action == 'send_datapolicy') {
  118. $object->fetch(GETPOST('id'));
  119. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  120. require_once DOL_DOCUMENT_ROOT.'/datapolicy/class/datapolicy.class.php';
  121. DataPolicy::sendMailDataPolicyContact($object);
  122. } elseif ($parameters['currentcontext'] == 'membercard' && $action == 'send_datapolicy') {
  123. $object->fetch(GETPOST('id'));
  124. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  125. require_once DOL_DOCUMENT_ROOT.'/datapolicy/class/datapolicy.class.php';
  126. DataPolicy::sendMailDataPolicyAdherent($object);
  127. } elseif ($parameters['currentcontext'] == 'thirdpartycard' && $action == 'send_datapolicy') {
  128. $object->fetch(GETPOST('socid'));
  129. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  130. require_once DOL_DOCUMENT_ROOT.'/datapolicy/class/datapolicy.class.php';
  131. DataPolicy::sendMailDataPolicyCompany($object);
  132. }
  133. if (!$error) {
  134. $this->results = array('myreturn' => 999);
  135. $this->resprints = 'A text to show';
  136. return 0; // or return 1 to replace standard code
  137. } else {
  138. $this->errors[] = 'Error message';
  139. return -1;
  140. }
  141. }
  142. /**
  143. * Overloading the doActions function : replacing the parent's function with the one below
  144. *
  145. * @param array $parameters Hook metadatas (context, etc...)
  146. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  147. * @param string $action Current action (if set). Generally create or edit or null
  148. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  149. * @return int < 0 on error, 0 on success, 1 to replace standard code
  150. */
  151. public function doMassActions($parameters, &$object, &$action, $hookmanager)
  152. {
  153. global $conf, $user, $langs;
  154. $error = 0; // Error counter
  155. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  156. //if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
  157. // // do something only for the context 'somecontext1' or 'somecontext2'
  158. // foreach ($parameters['toselect'] as $objectid) {
  159. // // Do action on each object id
  160. // }
  161. //}
  162. if (!$error) {
  163. $this->results = array('myreturn' => 999);
  164. $this->resprints = 'A text to show';
  165. return 0; // or return 1 to replace standard code
  166. } else {
  167. $this->errors[] = 'Error message';
  168. return -1;
  169. }
  170. }
  171. /**
  172. * Overloading the addMoreMassActions function : replacing the parent's function with the one below
  173. *
  174. * @param array $parameters Hook metadatas (context, etc...)
  175. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  176. * @param string $action Current action (if set). Generally create or edit or null
  177. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  178. * @return int < 0 on error, 0 on success, 1 to replace standard code
  179. */
  180. public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
  181. {
  182. global $conf, $user, $langs;
  183. $error = 0; // Error counter
  184. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  185. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  186. $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("datapolicyMassAction").'</option>';
  187. }
  188. if (!$error) {
  189. return 0; // or return 1 to replace standard code
  190. } else {
  191. $this->errors[] = 'Error message';
  192. return -1;
  193. }
  194. }
  195. /**
  196. * Execute action
  197. *
  198. * @param array $parameters Array of parameters
  199. * @param Object $object Object output on PDF
  200. * @param string $action 'add', 'update', 'view'
  201. * @return int <0 if KO,
  202. * =0 if OK but we want to process standard actions too,
  203. * >0 if OK and we want to replace standard actions.
  204. */
  205. public function beforePDFCreation($parameters, &$object, &$action)
  206. {
  207. global $conf, $user, $langs;
  208. global $hookmanager;
  209. $outputlangs = $langs;
  210. $ret = 0;
  211. $deltemp = array();
  212. dol_syslog(get_class($this).'::executeHooks action='.$action);
  213. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  214. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  215. }
  216. return $ret;
  217. }
  218. /**
  219. * addMoreActionsButtons
  220. *
  221. * @param array $parameters array of parameters
  222. * @param Object $object Object
  223. * @param string $action Actions
  224. * @param HookManager $hookmanager Hook manager
  225. * @return void
  226. */
  227. public function addMoreActionsButtons($parameters, &$object, &$action, $hookmanager)
  228. {
  229. global $conf, $user, $langs;
  230. $langs->load('datapolicy@datapolicy');
  231. if (!empty($conf->global->DATAPOLICY_ENABLE_EMAILS)) {
  232. $dialog = '<div id="dialogdatapolicy" style="display:none;" title="'.$langs->trans('DATAPOLICY_PORTABILITE_TITLE').'">';
  233. $dialog .= '<div class="confirmmessage">'.img_help('', '').' '.$langs->trans('DATAPOLICY_PORTABILITE_CONFIRMATION').'</div>';
  234. $dialog .= "</div>";
  235. $dialog .= '<script>
  236. $( function() {
  237. $("#rpgpdbtn").on("click", function(){
  238. var href = $(this).attr("href");
  239. $( "#dialogdatapolicy" ).dialog({
  240. modal: true,
  241. buttons: {
  242. "OK": function() {
  243. window.open(href);
  244. $( this ).dialog( "close" );
  245. },
  246. "' . $langs->trans("Cancel").'": function() {
  247. $( this ).dialog( "close" );
  248. }
  249. }
  250. });
  251. return false;
  252. });
  253. } );
  254. </script>';
  255. echo $dialog;
  256. // TODO Replace test of hardcoded values
  257. 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) {
  258. 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>';
  259. } elseif (!empty($object->mail) && empty($object->array_options['options_datapolicy_send']) && $parameters['currentcontext'] == 'membercard') {
  260. 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>';
  261. } elseif (!empty($object->mail) && empty($object->array_options['options_datapolicy_send']) && $parameters['currentcontext'] == 'contactcard') {
  262. 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>';
  263. }
  264. }
  265. }
  266. /**
  267. * printCommonFooter
  268. *
  269. * @param array $parameters array of parameters
  270. * @param Object $object Object
  271. * @param string $action Actions
  272. * @param HookManager $hookmanager Hook manager
  273. * @return int
  274. */
  275. public function printCommonFooter($parameters, &$object, &$action, $hookmanager)
  276. {
  277. global $conf, $user, $langs;
  278. $jsscript = '';
  279. if ($parameters['currentcontext'] == 'thirdpartycard') {
  280. if (GETPOST('action') == 'create' || GETPOST('action') == 'edit' || GETPOST('action') == '') {
  281. $jsscript .= '<script>';
  282. $jsscript .= "var elementToHide = 'tr.societe_extras_datapolicy_consentement, tr.societe_extras_datapolicy_opposition_traitement, tr.societe_extras_datapolicy_opposition_prospection';".PHP_EOL;
  283. $jsscript .= "var forme_juridique = [".PHP_EOL;
  284. $jsscript .= "11, 12, 13, 15, 17, 18, 19, 35, 60, 200, 311, 312, 316, 401, 600, 700, 1005".PHP_EOL;
  285. $jsscript .= "];".PHP_EOL;
  286. $jsscript .= "function hideRgPD() {".PHP_EOL;
  287. $jsscript .= " if ($('#typent_id').val() == 8 || forme_juridique.indexOf(parseInt($('#forme_juridique_code').val())) > -1) {".PHP_EOL;
  288. $jsscript .= " console.log(elementToHide);".PHP_EOL;
  289. $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;
  290. $jsscript .= "hideRgPD();".PHP_EOL;
  291. $jsscript .= "$('#forme_juridique_code, #typent_id').change(function(){ hideRgPD(); });".PHP_EOL;
  292. $jsscript .= '</script>';
  293. } elseif (GETPOST('action') == 'confirm_delete' && GETPOST('confirm') == 'yes' && GETPOST('socid') > 0) {
  294. // La suppression n'a pas été possible
  295. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  296. $societe = new Societe($this->db);
  297. $societe->fetch(GETPOST('socid'));
  298. // On vérifie si il est utilisé
  299. 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'))) {
  300. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  301. $form = new Form($this->db);
  302. 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);
  303. }
  304. }
  305. if (GETPOST('socid')) {
  306. /* Removed due to awful harcoded values
  307. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  308. $societe = new Societe($this->db);
  309. $societe->fetch(GETPOST('socid'));
  310. 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) {
  311. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  312. $jsscript .= '<script>';
  313. $jsscript .= "var elementToHide = 'td.societe_extras_datapolicy_opposition_traitement, td.societe_extras_datapolicy_opposition_prospection, td.societe_extras_datapolicy_consentement';".PHP_EOL;
  314. $jsscript .= "$(elementToHide).parent('tr').hide();".PHP_EOL;
  315. $jsscript .= '</script>';
  316. }
  317. */
  318. }
  319. } elseif ($parameters['currentcontext'] == 'contactcard') {
  320. if (GETPOST('action') == 'create' || GETPOST('action') == 'edit') {
  321. $jsscript .= '<script>';
  322. $jsscript .= "$('#options_datapolicy_opposition_traitement, #options_datapolicy_opposition_prospection, input[name=\"options_datapolicy_opposition_traitement\"], input[name=\"options_datapolicy_opposition_prospection\"]').change(function(){
  323. 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')) {
  324. $('#no_email').val(1);
  325. }
  326. });";
  327. $jsscript .= '</script>';
  328. }
  329. }
  330. $this->resprint = $jsscript;
  331. return 0;
  332. }
  333. }