ticket.lib.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. <?php
  2. /* Copyright (C) 2013-2018 Jean-François FERRY <hello@librethic.io>
  3. * Copyright (C) 2016 Christophe Battarel <christophe@altairis.fr>
  4. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file core/lib/ticket.lib.php
  21. * \ingroup ticket
  22. * \brief This file is a library for Ticket module
  23. */
  24. /**
  25. * Build tabs for admin page
  26. *
  27. * @return array
  28. */
  29. function ticketAdminPrepareHead()
  30. {
  31. global $langs, $conf;
  32. $langs->load("ticket");
  33. $h = 0;
  34. $head = array();
  35. $head[$h][0] = DOL_URL_ROOT.'/admin/ticket.php';
  36. $head[$h][1] = $langs->trans("TicketSettings");
  37. $head[$h][2] = 'settings';
  38. $h++;
  39. $head[$h][0] = DOL_URL_ROOT.'/admin/ticket_extrafields.php';
  40. $head[$h][1] = $langs->trans("ExtraFieldsTicket");
  41. $head[$h][2] = 'attributes';
  42. $h++;
  43. $head[$h][0] = DOL_URL_ROOT.'/admin/ticket_public.php';
  44. $head[$h][1] = $langs->trans("PublicInterface");
  45. $head[$h][2] = 'public';
  46. $h++;
  47. // Show more tabs from modules
  48. // Entries must be declared in modules descriptor with line
  49. //$this->tabs = array(
  50. // 'entity:+tabname:Title:@ticket:/ticket/mypage.php?id=__ID__'
  51. //); // to add new tab
  52. //$this->tabs = array(
  53. // 'entity:-tabname:Title:@ticket:/ticket/mypage.php?id=__ID__'
  54. //); // to remove a tab
  55. complete_head_from_modules($conf, $langs, null, $head, $h, 'ticketadmin');
  56. return $head;
  57. }
  58. /**
  59. * Build tabs for a Ticket object
  60. *
  61. * @param Ticket $object Object Ticket
  62. * @return array Array of tabs
  63. */
  64. function ticket_prepare_head($object)
  65. {
  66. global $db, $langs, $conf, $user;
  67. $h = 0;
  68. $head = array();
  69. $head[$h][0] = DOL_URL_ROOT.'/ticket/card.php?action=view&track_id='.$object->track_id;
  70. $head[$h][1] = $langs->trans("Card");
  71. $head[$h][2] = 'tabTicket';
  72. $h++;
  73. if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && empty($user->socid))
  74. {
  75. $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
  76. $head[$h][0] = DOL_URL_ROOT.'/ticket/contact.php?track_id='.$object->track_id;
  77. $head[$h][1] = $langs->trans('ContactsAddresses');
  78. if ($nbContact > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
  79. $head[$h][2] = 'contact';
  80. $h++;
  81. }
  82. complete_head_from_modules($conf, $langs, $object, $head, $h, 'ticket');
  83. // Attached files
  84. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  85. $upload_dir = $conf->ticket->dir_output."/".$object->ref;
  86. $nbFiles = count(dol_dir_list($upload_dir, 'files'));
  87. $head[$h][0] = dol_buildpath('/ticket/document.php', 1).'?id='.$object->id;
  88. $head[$h][1] = $langs->trans("Documents");
  89. if ($nbFiles > 0) {
  90. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbFiles.'</span>';
  91. }
  92. $head[$h][2] = 'tabTicketDocument';
  93. $h++;
  94. // History
  95. $ticketViewType = "messaging";
  96. if (empty($_SESSION['ticket-view-type'])) {
  97. $_SESSION['ticket-view-type'] = $ticketViewType;
  98. }
  99. else {
  100. $ticketViewType = $_SESSION['ticket-view-type'];
  101. }
  102. if ($ticketViewType == "messaging") {
  103. $head[$h][0] = DOL_URL_ROOT.'/ticket/messaging.php?track_id='.$object->track_id;
  104. }
  105. else {
  106. // $ticketViewType == "list"
  107. $head[$h][0] = DOL_URL_ROOT.'/ticket/agenda.php?track_id='.$object->track_id;
  108. }
  109. $head[$h][1] = $langs->trans('Events');
  110. if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read)))
  111. {
  112. $head[$h][1] .= '/';
  113. $head[$h][1] .= $langs->trans("Agenda");
  114. }
  115. $head[$h][2] = 'tabTicketLogs';
  116. $h++;
  117. complete_head_from_modules($conf, $langs, $object, $head, $h, 'ticket', 'remove');
  118. return $head;
  119. }
  120. /**
  121. * Return string with full Url. The file qualified is the one defined by relative path in $object->last_main_doc
  122. *
  123. * @param Object $object Object
  124. * @return string Url string
  125. */
  126. function showDirectPublicLink($object)
  127. {
  128. global $conf, $langs;
  129. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  130. $email = CMailFile::getValidAddress($object->origin_email, 2);
  131. $url = '';
  132. if ($email)
  133. {
  134. $url = dol_buildpath('/public/ticket/view.php', 3).'?track_id='.$object->track_id.'&email='.$email;
  135. }
  136. $out = '';
  137. if (empty($conf->global->TICKET_ENABLE_PUBLIC_INTERFACE))
  138. {
  139. $out .= '<span class="opacitymedium">'.$langs->trans("PublicInterfaceNotEnabled").'</span>';
  140. }
  141. else
  142. {
  143. $out .= img_picto('', 'object_globe.png').' '.$langs->trans("TicketPublicAccess").':<br>';
  144. if ($url)
  145. {
  146. $out .= '<input type="text" id="directpubliclink" class="quatrevingtpercent" value="'.$url.'">';
  147. $out .= ajax_autoselect("directpubliclink", 0);
  148. }
  149. else
  150. {
  151. $out .= '<span class="opacitymedium">'.$langs->trans("TicketNotCreatedFromPublicInterface").'</span>';
  152. }
  153. }
  154. return $out;
  155. }
  156. /**
  157. * Generate a random id
  158. *
  159. * @param int $car Length of string to generate key
  160. * @return string
  161. */
  162. function generate_random_id($car = 16)
  163. {
  164. $string = "";
  165. $chaine = "abcdefghijklmnopqrstuvwxyz123456789";
  166. srand((double) microtime() * 1000000);
  167. for ($i = 0; $i < $car; $i++) {
  168. $string .= $chaine[rand() % strlen($chaine)];
  169. }
  170. return $string;
  171. }
  172. /**
  173. * Show header for public pages
  174. *
  175. * @param string $title Title
  176. * @param string $head Head array
  177. * @param int $disablejs More content into html header
  178. * @param int $disablehead More content into html header
  179. * @param array $arrayofjs Array of complementary js files
  180. * @param array $arrayofcss Array of complementary css files
  181. * @return void
  182. */
  183. function llxHeaderTicket($title, $head = "", $disablejs = 0, $disablehead = 0, $arrayofjs = '', $arrayofcss = '')
  184. {
  185. global $user, $conf, $langs, $mysoc;
  186. top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss); // Show html headers
  187. print '<body id="mainbody" class="publicnewticketform">';
  188. // Define urllogo
  189. $width = 0;
  190. if (!empty($conf->global->TICKET_SHOW_COMPANY_LOGO) || !empty($conf->global->TICKET_PUBLIC_INTERFACE_TOPIC)) {
  191. // Print logo
  192. if (!empty($conf->global->TICKET_SHOW_COMPANY_LOGO))
  193. {
  194. $urllogo = DOL_URL_ROOT.'/theme/common/login_logo.png';
  195. if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
  196. $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
  197. $width = 150;
  198. } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
  199. $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/'.$mysoc->logo);
  200. $width = 150;
  201. } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.svg')) {
  202. $urllogo = DOL_URL_ROOT.'/theme/dolibarr_logo.svg';
  203. }
  204. }
  205. }
  206. print '<div class="center">';
  207. // Output html code for logo
  208. if ($urllogo || !empty($conf->global->TICKET_PUBLIC_INTERFACE_TOPIC))
  209. {
  210. print '<div class="backgreypublicpayment">';
  211. print '<div class="logopublicpayment">';
  212. if ($urllogo) {
  213. print '<a href="'.($conf->global->TICKET_URL_PUBLIC_INTERFACE ? $conf->global->TICKET_URL_PUBLIC_INTERFACE : dol_buildpath('/public/ticket/index.php', 1)).'">';
  214. print '<img id="dolpaymentlogo" src="'.$urllogo.'"';
  215. if ($width) print ' width="'.$width.'"';
  216. print '>';
  217. print '</a>';
  218. }
  219. if (!empty($conf->global->TICKET_PUBLIC_INTERFACE_TOPIC)) {
  220. print '<div class="clearboth"></div><strong>'.($conf->global->TICKET_PUBLIC_INTERFACE_TOPIC ? $conf->global->TICKET_PUBLIC_INTERFACE_TOPIC : $langs->trans("TicketSystem")).'</strong>';
  221. }
  222. print '</div>';
  223. if (empty($conf->global->MAIN_HIDE_POWERED_BY)) {
  224. print '<div class="poweredbypublicpayment opacitymedium right"><a href="https://www.dolibarr.org" target="dolibarr">'.$langs->trans("PoweredBy").'<br><img src="'.DOL_URL_ROOT.'/theme/dolibarr_logo.svg" width="80px"></a></div>';
  225. }
  226. print '</div>';
  227. }
  228. print '</div>';
  229. print '<div class="ticketlargemargin">';
  230. }
  231. /**
  232. * Show html area with actions for ticket messaging.
  233. * Note: Global parameter $param must be defined.
  234. *
  235. * @param Conf $conf Object conf
  236. * @param Translate $langs Object langs
  237. * @param DoliDB $db Object db
  238. * @param mixed $filterobj Filter on object Adherent|Societe|Project|Product|CommandeFournisseur|Dolresource|Ticket|... to list events linked to an object
  239. * @param Contact $objcon Filter on object contact to filter events on a contact
  240. * @param int $noprint Return string but does not output it
  241. * @param string $actioncode Filter on actioncode
  242. * @param string $donetodo Filter on event 'done' or 'todo' or ''=nofilter (all).
  243. * @param array $filters Filter on other fields
  244. * @param string $sortfield Sort field
  245. * @param string $sortorder Sort order
  246. * @return string|void Return html part or void if noprint is 1
  247. */
  248. function show_ticket_messaging($conf, $langs, $db, $filterobj, $objcon = '', $noprint = 0, $actioncode = '', $donetodo = 'done', $filters = array(), $sortfield = 'a.datep,a.id', $sortorder = 'DESC')
  249. {
  250. global $user, $conf;
  251. global $form;
  252. global $param, $massactionbutton;
  253. dol_include_once('/comm/action/class/actioncomm.class.php');
  254. // Check parameters
  255. if (!is_object($filterobj) && !is_object($objcon)) dol_print_error('', 'BadParameter');
  256. $out = '';
  257. $histo = array();
  258. $numaction = 0;
  259. $now = dol_now('tzuser');
  260. // Open DSI -- Fix order by -- Begin
  261. $sortfield_list = explode(',', $sortfield);
  262. $sortfield_label_list = array('a.id' => 'id', 'a.datep' => 'dp', 'a.percent' => 'percent');
  263. $sortfield_new_list = array();
  264. foreach ($sortfield_list as $sortfield_value) {
  265. $sortfield_new_list[] = $sortfield_label_list[trim($sortfield_value)];
  266. }
  267. $sortfield_new = implode(',', $sortfield_new_list);
  268. if (!empty($conf->agenda->enabled))
  269. {
  270. // Recherche histo sur actioncomm
  271. if (is_object($objcon) && $objcon->id > 0) {
  272. $sql = "SELECT DISTINCT a.id, a.label as label,";
  273. }
  274. else
  275. {
  276. $sql = "SELECT a.id, a.label as label,";
  277. }
  278. $sql .= " a.datep as dp,";
  279. $sql .= " a.note as message,";
  280. $sql .= " a.datep2 as dp2,";
  281. $sql .= " a.percent as percent, 'action' as type,";
  282. $sql .= " a.fk_element, a.elementtype,";
  283. $sql .= " a.fk_contact,";
  284. $sql .= " c.code as acode, c.libelle as alabel, c.picto as apicto,";
  285. $sql .= " u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname";
  286. if (is_object($filterobj) && get_class($filterobj) == 'Societe') $sql .= ", sp.lastname, sp.firstname";
  287. elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') $sql .= ", m.lastname, m.firstname";
  288. elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') $sql .= ", o.ref";
  289. elseif (is_object($filterobj) && get_class($filterobj) == 'Product') $sql .= ", o.ref";
  290. elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql .= ", o.ref";
  291. elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') $sql .= ", o.ref";
  292. elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') $sql .= ", o.ref";
  293. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a";
  294. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_action";
  295. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_actioncomm as c ON a.fk_action = c.id";
  296. $force_filter_contact = false;
  297. if (is_object($objcon) && $objcon->id > 0) {
  298. $force_filter_contact = true;
  299. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."actioncomm_resources as r ON a.id = r.fk_actioncomm";
  300. $sql .= " AND r.element_type = '".$db->escape($objcon->table_element)."' AND r.fk_element = ".$objcon->id;
  301. }
  302. if (is_object($filterobj) && get_class($filterobj) == 'Societe') $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON a.fk_contact = sp.rowid";
  303. elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') {
  304. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."element_resources as er";
  305. $sql .= " ON er.resource_type = 'dolresource'";
  306. $sql .= " AND er.element_id = a.id";
  307. $sql .= " AND er.resource_id = ".$filterobj->id;
  308. }
  309. elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') $sql .= ", ".MAIN_DB_PREFIX."adherent as m";
  310. elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseur as o";
  311. elseif (is_object($filterobj) && get_class($filterobj) == 'Product') $sql .= ", ".MAIN_DB_PREFIX."product as o";
  312. elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql .= ", ".MAIN_DB_PREFIX."ticket as o";
  313. elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') $sql .= ", ".MAIN_DB_PREFIX."bom_bom as o";
  314. elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') $sql .= ", ".MAIN_DB_PREFIX."contrat as o";
  315. $sql .= " WHERE a.entity IN (".getEntity('agenda').")";
  316. if ($force_filter_contact === false) {
  317. if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur')) && $filterobj->id) $sql .= " AND a.fk_soc = ".$filterobj->id;
  318. elseif (is_object($filterobj) && get_class($filterobj) == 'Project' && $filterobj->id) $sql .= " AND a.fk_project = ".$filterobj->id;
  319. elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent')
  320. {
  321. $sql .= " AND a.fk_element = m.rowid AND a.elementtype = 'member'";
  322. if ($filterobj->id) $sql .= " AND a.fk_element = ".$filterobj->id;
  323. }
  324. elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur')
  325. {
  326. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'order_supplier'";
  327. if ($filterobj->id) $sql .= " AND a.fk_element = ".$filterobj->id;
  328. }
  329. elseif (is_object($filterobj) && get_class($filterobj) == 'Product')
  330. {
  331. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'product'";
  332. if ($filterobj->id) $sql .= " AND a.fk_element = ".$filterobj->id;
  333. }
  334. elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket')
  335. {
  336. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'ticket'";
  337. if ($filterobj->id) $sql .= " AND a.fk_element = ".$filterobj->id;
  338. }
  339. elseif (is_object($filterobj) && get_class($filterobj) == 'BOM')
  340. {
  341. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'bom'";
  342. if ($filterobj->id) $sql .= " AND a.fk_element = ".$filterobj->id;
  343. }
  344. elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat')
  345. {
  346. $sql .= " AND a.fk_element = o.rowid AND a.elementtype = 'contract'";
  347. if ($filterobj->id) $sql .= " AND a.fk_element = ".$filterobj->id;
  348. }
  349. }
  350. // Condition on actioncode
  351. if (!empty($actioncode))
  352. {
  353. if (empty($conf->global->AGENDA_USE_EVENT_TYPE))
  354. {
  355. if ($actioncode == 'AC_NON_AUTO') $sql .= " AND c.type != 'systemauto'";
  356. elseif ($actioncode == 'AC_ALL_AUTO') $sql .= " AND c.type = 'systemauto'";
  357. else
  358. {
  359. if ($actioncode == 'AC_OTH') $sql .= " AND c.type != 'systemauto'";
  360. elseif ($actioncode == 'AC_OTH_AUTO') $sql .= " AND c.type = 'systemauto'";
  361. }
  362. }
  363. else
  364. {
  365. if ($actioncode == 'AC_NON_AUTO') $sql .= " AND c.type != 'systemauto'";
  366. elseif ($actioncode == 'AC_ALL_AUTO') $sql .= " AND c.type = 'systemauto'";
  367. else $sql .= " AND c.code = '".$db->escape($actioncode)."'";
  368. }
  369. }
  370. if ($donetodo == 'todo') $sql .= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  371. elseif ($donetodo == 'done') $sql .= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  372. if (is_array($filters) && $filters['search_agenda_label']) $sql .= natural_search('a.label', $filters['search_agenda_label']);
  373. }
  374. // Add also event from emailings. TODO This should be replaced by an automatic event ? May be it's too much for very large emailing.
  375. if (!empty($conf->mailing->enabled) && !empty($objcon->email)
  376. && (empty($actioncode) || $actioncode == 'AC_OTH_AUTO' || $actioncode == 'AC_EMAILING'))
  377. {
  378. $langs->load("mails");
  379. $sql2 = "SELECT m.rowid as id, m.titre as label, mc.date_envoi as dp, mc.date_envoi as dp2, '100' as percent, 'mailing' as type";
  380. $sql2 .= ", '' as fk_element, '' as elementtype, '' as contact_id";
  381. $sql2 .= ", 'AC_EMAILING' as acode, '' as alabel, '' as apicto";
  382. $sql2 .= ", u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname"; // User that valid action
  383. if (is_object($filterobj) && get_class($filterobj) == 'Societe') $sql2 .= ", '' as lastname, '' as firstname";
  384. elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') $sql2 .= ", '' as lastname, '' as firstname";
  385. elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') $sql2 .= ", '' as ref";
  386. elseif (is_object($filterobj) && get_class($filterobj) == 'Product') $sql2 .= ", '' as ref";
  387. elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql2 .= ", '' as ref";
  388. $sql2 .= " FROM ".MAIN_DB_PREFIX."mailing as m, ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."user as u";
  389. $sql2 .= " WHERE mc.email = '".$db->escape($objcon->email)."'"; // Search is done on email.
  390. $sql2 .= " AND mc.statut = 1";
  391. $sql2 .= " AND u.rowid = m.fk_user_valid";
  392. $sql2 .= " AND mc.fk_mailing=m.rowid";
  393. }
  394. if (!empty($sql) && !empty($sql2)) {
  395. $sql = $sql." UNION ".$sql2;
  396. } elseif (empty($sql) && !empty($sql2)) {
  397. $sql = $sql2;
  398. }
  399. //TODO Add limit in nb of results
  400. $sql .= $db->order($sortfield_new, $sortorder);
  401. dol_syslog("company.lib::show_actions_done", LOG_DEBUG);
  402. $resql = $db->query($sql);
  403. if ($resql)
  404. {
  405. $i = 0;
  406. $num = $db->num_rows($resql);
  407. while ($i < $num)
  408. {
  409. $obj = $db->fetch_object($resql);
  410. if ($obj->type == 'action') {
  411. $contactaction = new ActionComm($db);
  412. $contactaction->id = $obj->id;
  413. $result = $contactaction->fetchResources();
  414. if ($result < 0) {
  415. dol_print_error($db);
  416. setEventMessage("company.lib::show_actions_done Error fetch ressource", 'errors');
  417. }
  418. //if ($donetodo == 'todo') $sql.= " AND ((a.percent >= 0 AND a.percent < 100) OR (a.percent = -1 AND a.datep > '".$db->idate($now)."'))";
  419. //elseif ($donetodo == 'done') $sql.= " AND (a.percent = 100 OR (a.percent = -1 AND a.datep <= '".$db->idate($now)."'))";
  420. $tododone = '';
  421. if (($obj->percent >= 0 and $obj->percent < 100) || ($obj->percent == -1 && $obj->datep > $now)) $tododone = 'todo';
  422. $histo[$numaction] = array(
  423. 'type'=>$obj->type,
  424. 'tododone'=>$tododone,
  425. 'id'=>$obj->id,
  426. 'datestart'=>$db->jdate($obj->dp),
  427. 'dateend'=>$db->jdate($obj->dp2),
  428. 'note'=>$obj->label,
  429. 'message'=>$obj->message,
  430. 'percent'=>$obj->percent,
  431. 'userid'=>$obj->user_id,
  432. 'login'=>$obj->user_login,
  433. 'userfirstname'=>$obj->user_firstname,
  434. 'userlastname'=>$obj->user_lastname,
  435. 'userphoto'=>$obj->user_photo,
  436. 'contact_id'=>$obj->fk_contact,
  437. 'socpeopleassigned' => $contactaction->socpeopleassigned,
  438. 'lastname'=>$obj->lastname,
  439. 'firstname'=>$obj->firstname,
  440. 'fk_element'=>$obj->fk_element,
  441. 'elementtype'=>$obj->elementtype,
  442. // Type of event
  443. 'acode'=>$obj->acode,
  444. 'alabel'=>$obj->alabel,
  445. 'libelle'=>$obj->alabel, // deprecated
  446. 'apicto'=>$obj->apicto
  447. );
  448. } else {
  449. $histo[$numaction] = array(
  450. 'type'=>$obj->type,
  451. 'tododone'=>'done',
  452. 'id'=>$obj->id,
  453. 'datestart'=>$db->jdate($obj->dp),
  454. 'dateend'=>$db->jdate($obj->dp2),
  455. 'note'=>$obj->label,
  456. 'message'=>$obj->message,
  457. 'percent'=>$obj->percent,
  458. 'acode'=>$obj->acode,
  459. 'userid'=>$obj->user_id,
  460. 'login'=>$obj->user_login,
  461. 'userfirstname'=>$obj->user_firstname,
  462. 'userlastname'=>$obj->user_lastname,
  463. 'userphoto'=>$obj->user_photo
  464. );
  465. }
  466. $numaction++;
  467. $i++;
  468. }
  469. }
  470. else
  471. {
  472. dol_print_error($db);
  473. }
  474. if (!empty($conf->agenda->enabled) || (!empty($conf->mailing->enabled) && !empty($objcon->email)))
  475. {
  476. $delay_warning = $conf->global->MAIN_DELAY_ACTIONS_TODO * 24 * 60 * 60;
  477. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  478. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  479. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
  480. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  481. $formactions = new FormActions($db);
  482. $actionstatic = new ActionComm($db);
  483. $userstatic = new User($db);
  484. $contactstatic = new Contact($db);
  485. $userGetNomUrlCache = array();
  486. $out .= '<div class="filters-container" >';
  487. $out .= '<form name="listactionsfilter" class="listactionsfilter" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  488. if ($objcon && get_class($objcon) == 'Contact' &&
  489. (is_null($filterobj) || get_class($filterobj) == 'Societe'))
  490. {
  491. $out .= '<input type="hidden" name="id" value="'.$objcon->id.'" />';
  492. }
  493. else
  494. {
  495. $out .= '<input type="hidden" name="id" value="'.$filterobj->id.'" />';
  496. }
  497. if ($filterobj && get_class($filterobj) == 'Societe') $out .= '<input type="hidden" name="socid" value="'.$filterobj->id.'" />';
  498. $out .= "\n";
  499. $out .= '<div class="div-table-responsive-no-min">';
  500. $out .= '<table class="noborder borderbottom centpercent">';
  501. $out .= '<tr class="liste_titre">';
  502. //$out.='<td class="liste_titre">';
  503. $out .= getTitleFieldOfList('Date', 0, $_SERVER["PHP_SELF"], 'a.datep', '', $param, '', $sortfield, $sortorder, '')."\n";
  504. //$out.='</td>';
  505. $out .= '<th class="liste_titre"><strong>'.$langs->trans("Search").' : </strong></th>';
  506. if ($donetodo)
  507. {
  508. $out .= '<th class="liste_titre"></th>';
  509. }
  510. $out .= '<th class="liste_titre">'.$langs->trans("Type").' ';
  511. $out .= $formactions->select_type_actions($actioncode, "actioncode", '', empty($conf->global->AGENDA_USE_EVENT_TYPE) ? 1 : -1, 0, 0, 1);
  512. $out .= '</th>';
  513. $out .= '<th class="liste_titre maxwidth100onsmartphone">';
  514. $out .= $langs->trans("Label").' ';
  515. $out .= '<input type="text" class="maxwidth100onsmartphone" name="search_agenda_label" value="'.$filters['search_agenda_label'].'">';
  516. $out .= '</th>';
  517. $out .= '<th class="liste_titre width50 middle">';
  518. $searchpicto = $form->showFilterAndCheckAddButtons($massactionbutton ? 1 : 0, 'checkforselect', 1);
  519. $out .= $searchpicto;
  520. $out .= '</th>';
  521. $out .= '</tr>';
  522. $out .= '</table>';
  523. $out .= '</form>';
  524. $out .= '</div>';
  525. $out .= "\n";
  526. $out .= '<ul class="timeline">';
  527. if ($donetodo)
  528. {
  529. $tmp = '';
  530. if (get_class($filterobj) == 'Societe') $tmp .= '<a href="'.DOL_URL_ROOT.'/comm/action/list.php?socid='.$filterobj->id.'&amp;status=done">';
  531. $tmp .= ($donetodo != 'done' ? $langs->trans("ActionsToDoShort") : '');
  532. $tmp .= ($donetodo != 'done' && $donetodo != 'todo' ? ' / ' : '');
  533. $tmp .= ($donetodo != 'todo' ? $langs->trans("ActionsDoneShort") : '');
  534. //$out.=$langs->trans("ActionsToDoShort").' / '.$langs->trans("ActionsDoneShort");
  535. if (get_class($filterobj) == 'Societe') $tmp .= '</a>';
  536. $out .= getTitleFieldOfList($tmp);
  537. }
  538. //require_once DOL_DOCUMENT_ROOT.'/comm/action/class/cactioncomm.class.php';
  539. //$caction=new CActionComm($db);
  540. //$arraylist=$caction->liste_array(1, 'code', '', (empty($conf->global->AGENDA_USE_EVENT_TYPE)?1:0), '', 1);
  541. $actualCycleDate = false;
  542. foreach ($histo as $key=>$value)
  543. {
  544. $actionstatic->fetch($histo[$key]['id']); // TODO Do we need this, we already have a lot of data of line into $histo
  545. $actionstatic->type_picto = $histo[$key]['apicto'];
  546. $actionstatic->type_code = $histo[$key]['acode'];
  547. $url = DOL_URL_ROOT.'/comm/action/card.php?id='.$histo[$key]['id'];
  548. $tmpa = dol_getdate($histo[$key]['datestart'], false);
  549. if ($actualCycleDate !== $tmpa['year'].'-'.$tmpa['yday']) {
  550. $actualCycleDate = $tmpa['year'].'-'.$tmpa['yday'];
  551. $out .= '<!-- timeline time label -->';
  552. $out .= '<li class="time-label">';
  553. $out .= '<span class="timeline-badge-date">';
  554. $out .= dol_print_date($histo[$key]['datestart'], 'daytext', 'tzserver', $langs);
  555. $out .= '</span>';
  556. $out .= '</li>';
  557. $out .= '<!-- /.timeline-label -->';
  558. }
  559. $out .= '<!-- timeline item -->'."\n";
  560. $out .= '<li class="timeline-code-'.strtolower($actionstatic->code).'">';
  561. $out .= '<!-- timeline icon -->'."\n";
  562. $iconClass = 'fa fa-comments';
  563. $img_picto = '';
  564. $colorClass = '';
  565. $pictoTitle = '';
  566. if ($histo[$key]['percent'] == -1) {
  567. $colorClass = 'timeline-icon-not-applicble';
  568. $pictoTitle = $langs->trans('StatusNotApplicable');
  569. }
  570. elseif ($histo[$key]['percent'] == 0) {
  571. $colorClass = 'timeline-icon-todo';
  572. $pictoTitle = $langs->trans('StatusActionToDo').' (0%)';
  573. }
  574. elseif ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100) {
  575. $colorClass = 'timeline-icon-in-progress';
  576. $pictoTitle = $langs->trans('StatusActionInProcess').' ('.$histo[$key]['percent'].'%)';
  577. }
  578. elseif ($histo[$key]['percent'] >= 100) {
  579. $colorClass = 'timeline-icon-done';
  580. $pictoTitle = $langs->trans('StatusActionDone').' (100%)';
  581. }
  582. if ($actionstatic->code == 'AC_TICKET_CREATE') {
  583. $iconClass = 'fa fa-ticket';
  584. }
  585. elseif ($actionstatic->code == 'AC_TICKET_MODIFY') {
  586. $iconClass = 'fa fa-pencil';
  587. }
  588. elseif ($actionstatic->code == 'TICKET_MSG') {
  589. $iconClass = 'fa fa-comments';
  590. }
  591. elseif ($actionstatic->code == 'TICKET_MSG_PRIVATE') {
  592. $iconClass = 'fa fa-mask';
  593. }
  594. elseif (!empty($conf->global->AGENDA_USE_EVENT_TYPE))
  595. {
  596. if ($actionstatic->type_picto) $img_picto = img_picto('', $actionstatic->type_picto);
  597. else {
  598. if ($actionstatic->type_code == 'AC_RDV') $iconClass = 'fa fa-handshake';
  599. elseif ($actionstatic->type_code == 'AC_TEL') $iconClass = 'fa fa-phone';
  600. elseif ($actionstatic->type_code == 'AC_FAX') $iconClass = 'fa fa-fax';
  601. elseif ($actionstatic->type_code == 'AC_EMAIL') $iconClass = 'fa fa-envelope';
  602. elseif ($actionstatic->type_code == 'AC_INT') $iconClass = 'fa fa-shipping-fast';
  603. elseif ($actionstatic->type_code == 'AC_OTH_AUTO') $iconClass = 'fa fa-robot';
  604. elseif (!preg_match('/_AUTO/', $actionstatic->type_code)) $iconClass = 'fa fa-robot';
  605. }
  606. }
  607. $out .= '<i class="'.$iconClass.' '.$colorClass.'" title="'.$pictoTitle.'">'.$img_picto.'</i>'."\n";
  608. $out .= '<div class="timeline-item">'."\n";
  609. $out .= '<span class="timeline-header-action">';
  610. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'mailing') {
  611. $out .= '<a class="timeline-btn" href="'.DOL_URL_ROOT.'/comm/mailing/card.php?id='.$histo[$key]['id'].'">'.img_object($langs->trans("ShowEMailing"), "email").' ';
  612. $out .= $histo[$key]['id'];
  613. $out .= '</a> ';
  614. } else {
  615. $out .= $actionstatic->getNomUrl(1, -1, 'valignmiddle').' ';
  616. }
  617. //if ($user->rights->agenda->allactions->read || $actionstatic->authorid == $user->id)
  618. //{
  619. // $out.='<a href="'.$url.'" class="timeline-btn" title="'.$langs->trans('Show').'" ><i class="fa fa-calendar" ></i>'.$langs->trans('Show').'</a>';
  620. //}
  621. if ($user->rights->agenda->allactions->create ||
  622. (($actionstatic->authorid == $user->id || $actionstatic->userownerid == $user->id) && $user->rights->agenda->myactions->create))
  623. {
  624. $out .= '<a class="timeline-btn" href="'.DOL_MAIN_URL_ROOT.'/comm/action/card.php?action=edit&id='.$actionstatic->id.'"><i class="fa fa-pencil" title="'.$langs->trans("Modify").'" ></i></a>';
  625. }
  626. $out .= '</span>';
  627. // Date
  628. $out .= '<span class="time"><i class="fa fa-clock-o"></i> ';
  629. $out .= dol_print_date($histo[$key]['datestart'], 'dayhour');
  630. if ($histo[$key]['dateend'] && $histo[$key]['dateend'] != $histo[$key]['datestart'])
  631. {
  632. $tmpa = dol_getdate($histo[$key]['datestart'], true);
  633. $tmpb = dol_getdate($histo[$key]['dateend'], true);
  634. if ($tmpa['mday'] == $tmpb['mday'] && $tmpa['mon'] == $tmpb['mon'] && $tmpa['year'] == $tmpb['year']) $out .= '-'.dol_print_date($histo[$key]['dateend'], 'hour');
  635. else $out .= '-'.dol_print_date($histo[$key]['dateend'], 'dayhour');
  636. }
  637. $late = 0;
  638. if ($histo[$key]['percent'] == 0 && $histo[$key]['datestart'] && $histo[$key]['datestart'] < ($now - $delay_warning)) $late = 1;
  639. if ($histo[$key]['percent'] == 0 && !$histo[$key]['datestart'] && $histo[$key]['dateend'] && $histo[$key]['datestart'] < ($now - $delay_warning)) $late = 1;
  640. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && $histo[$key]['dateend'] && $histo[$key]['dateend'] < ($now - $delay_warning)) $late = 1;
  641. if ($histo[$key]['percent'] > 0 && $histo[$key]['percent'] < 100 && !$histo[$key]['dateend'] && $histo[$key]['datestart'] && $histo[$key]['datestart'] < ($now - $delay_warning)) $late = 1;
  642. if ($late) $out .= img_warning($langs->trans("Late")).' ';
  643. $out .= "</span>\n";
  644. // Ref
  645. $out .= '<h3 class="timeline-header">';
  646. // Author of event
  647. $out .= '<span class="messaging-author">';
  648. if ($histo[$key]['userid'] > 0)
  649. {
  650. if (!isset($userGetNomUrlCache[$histo[$key]['userid']])) { // is in cache ?
  651. $userstatic->fetch($histo[$key]['userid']);
  652. $userGetNomUrlCache[$histo[$key]['userid']] = $userstatic->getNomUrl(-1, '', 0, 0, 16, 0, 'firstelselast', '');
  653. }
  654. $out .= $userGetNomUrlCache[$histo[$key]['userid']];
  655. }
  656. $out .= '</span>';
  657. // Title
  658. $out .= ' <span class="messaging-title">';
  659. if ($actionstatic->code == 'TICKET_MSG') {
  660. $out .= $langs->trans('TicketNewMessage');
  661. }
  662. elseif ($actionstatic->code == 'TICKET_MSG_PRIVATE') {
  663. $out .= $langs->trans('TicketNewMessage').' <em>('.$langs->trans('Private').')</em>';
  664. } else {
  665. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'action') {
  666. $transcode = $langs->trans("Action".$histo[$key]['acode']);
  667. $libelle = ($transcode != "Action".$histo[$key]['acode'] ? $transcode : $histo[$key]['alabel']);
  668. $libelle = $histo[$key]['note'];
  669. $actionstatic->id = $histo[$key]['id'];
  670. $out .= dol_trunc($libelle, 120);
  671. }
  672. if (isset($histo[$key]['type']) && $histo[$key]['type'] == 'mailing') {
  673. $out .= '<a href="'.DOL_URL_ROOT.'/comm/mailing/card.php?id='.$histo[$key]['id'].'">'.img_object($langs->trans("ShowEMailing"), "email").' ';
  674. $transcode = $langs->trans("Action".$histo[$key]['acode']);
  675. $libelle = ($transcode != "Action".$histo[$key]['acode'] ? $transcode : 'Send mass mailing');
  676. $out .= dol_trunc($libelle, 120);
  677. }
  678. }
  679. $out .= '</span>';
  680. $out .= '</h3>';
  681. if (!empty($histo[$key]['message'])
  682. && $actionstatic->code != 'AC_TICKET_CREATE'
  683. && $actionstatic->code != 'AC_TICKET_MODIFY'
  684. )
  685. {
  686. $out .= '<div class="timeline-body">';
  687. $out .= $histo[$key]['message'];
  688. $out .= '</div>';
  689. }
  690. // Timeline footer
  691. $footer = '';
  692. // Contact for this action
  693. if (isset($histo[$key]['socpeopleassigned']) && is_array($histo[$key]['socpeopleassigned']) && count($histo[$key]['socpeopleassigned']) > 0) {
  694. $contactList = '';
  695. foreach ($histo[$key]['socpeopleassigned'] as $cid => $Tab) {
  696. $contact = new Contact($db);
  697. $result = $contact->fetch($cid);
  698. if ($result < 0)
  699. dol_print_error($db, $contact->error);
  700. if ($result > 0) {
  701. $contactList .= !empty($contactList) ? ', ' : '';
  702. $contactList .= $contact->getNomUrl(1);
  703. if (isset($histo[$key]['acode']) && $histo[$key]['acode'] == 'AC_TEL') {
  704. if (!empty($contact->phone_pro))
  705. $contactList .= '('.dol_print_phone($contact->phone_pro).')';
  706. }
  707. }
  708. }
  709. $footer .= $langs->trans('ActionOnContact').' : '.$contactList;
  710. }
  711. elseif (empty($objcon->id) && isset($histo[$key]['contact_id']) && $histo[$key]['contact_id'] > 0)
  712. {
  713. $contact = new Contact($db);
  714. $result = $contact->fetch($histo[$key]['contact_id']);
  715. if ($result < 0)
  716. dol_print_error($db, $contact->error);
  717. if ($result > 0) {
  718. $footer .= $contact->getNomUrl(1);
  719. if (isset($histo[$key]['acode']) && $histo[$key]['acode'] == 'AC_TEL') {
  720. if (!empty($contact->phone_pro))
  721. $footer .= '('.dol_print_phone($contact->phone_pro).')';
  722. }
  723. }
  724. }
  725. $documents = getTicketActionCommEcmList($actionstatic);
  726. if (!empty($documents))
  727. {
  728. $footer .= '<div class="timeline-documents-container">';
  729. foreach ($documents as $doc)
  730. {
  731. $footer .= '<span id="document_'.$doc->id.'" class="timeline-documents" ';
  732. $footer .= ' data-id="'.$doc->id.'" ';
  733. $footer .= ' data-path="'.$doc->filepath.'"';
  734. $footer .= ' data-filename="'.dol_escape_htmltag($doc->filename).'" ';
  735. $footer .= '>';
  736. $filePath = DOL_DATA_ROOT.'/'.$doc->filepath.'/'.$doc->filename;
  737. $mime = dol_mimetype($filePath);
  738. $file = $actionstatic->id.'/'.$doc->filename;
  739. $thumb = $actionstatic->id.'/thumbs/'.substr($doc->filename, 0, strrpos($doc->filename, '.')).'_mini'.substr($doc->filename, strrpos($doc->filename, '.'));
  740. $doclink = dol_buildpath('document.php', 1).'?modulepart=actions&attachment=0&file='.urlencode($file).'&entity='.$conf->entity;
  741. $viewlink = dol_buildpath('viewimage.php', 1).'?modulepart=actions&file='.urlencode($thumb).'&entity='.$conf->entity;
  742. $mimeAttr = ' mime="'.$mime.'" ';
  743. $class = '';
  744. if (in_array($mime, array('image/png', 'image/jpeg', 'application/pdf'))) {
  745. $class .= ' documentpreview';
  746. }
  747. $footer .= '<a href="'.$doclink.'" class="btn-link '.$class.'" target="_blank" '.$mimeAttr.' >';
  748. $footer .= img_mime($filePath).' '.$doc->filename;
  749. $footer .= '</a>';
  750. $footer .= '</span>';
  751. }
  752. $footer .= '</div>';
  753. }
  754. if (!empty($footer)) {
  755. $out .= '<div class="timeline-footer">'.$footer.'</div>';
  756. }
  757. $out .= '</div>'."\n"; // end timeline-item
  758. $out .= '</li>';
  759. $out .= '<!-- END timeline item -->';
  760. $i++;
  761. }
  762. $out .= "</ul>\n";
  763. }
  764. if ($noprint) return $out;
  765. else print $out;
  766. }
  767. /**
  768. * getTicketActionCommEcmList
  769. *
  770. * @param ActionComm $object Object ActionComm
  771. * @return array Array of documents in index table
  772. */
  773. function getTicketActionCommEcmList($object)
  774. {
  775. global $conf, $db;
  776. $documents = array();
  777. $sql = 'SELECT ecm.rowid as id, ecm.src_object_type, ecm.src_object_id, ecm.filepath, ecm.filename';
  778. $sql .= ' FROM '.MAIN_DB_PREFIX.'ecm_files ecm';
  779. $sql .= ' WHERE ecm.filepath = \'agenda/'.$object->id.'\'';
  780. //$sql.= ' ecm.src_object_type = \''.$object->element.'\' AND ecm.src_object_id = '.$object->id; // Actually upload file doesn't add type
  781. $sql .= ' ORDER BY ecm.position ASC';
  782. $resql = $db->query($sql);
  783. if ($resql) {
  784. if ($db->num_rows($resql)) {
  785. while ($obj = $db->fetch_object($resql)) {
  786. $documents[$obj->id] = $obj;
  787. }
  788. }
  789. }
  790. return $documents;
  791. }