cactioncomm.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  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 htdocs/comm/action/class/cactioncomm.class.php
  20. * \ingroup agenda
  21. * \brief File of class to manage type of agenda events
  22. */
  23. /**
  24. * Class to manage different types of events
  25. */
  26. class CActionComm
  27. {
  28. /**
  29. * @var string Error code (or message)
  30. */
  31. public $error = '';
  32. /**
  33. * @var DoliDB Database handler.
  34. */
  35. public $db;
  36. /**
  37. * @var int ID
  38. */
  39. public $id;
  40. /**
  41. * @var string code
  42. */
  43. public $code;
  44. /**
  45. * @var string type
  46. */
  47. public $type;
  48. /**
  49. * @var string label
  50. * @deprecated
  51. * @see $label
  52. */
  53. public $libelle;
  54. /**
  55. * @var string Type of agenda event label
  56. */
  57. public $label;
  58. /**
  59. * @var int active
  60. */
  61. public $active;
  62. /**
  63. * @var string color hex
  64. */
  65. public $color;
  66. /**
  67. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  68. */
  69. public $picto;
  70. /**
  71. * @var array array of type_actions
  72. */
  73. public $type_actions = array();
  74. /**
  75. * @var array Used to return value by some methods
  76. */
  77. public $liste_array;
  78. /**
  79. * Constructor
  80. *
  81. * @param DoliDB $db Database handler
  82. */
  83. public function __construct($db)
  84. {
  85. $this->db = $db;
  86. }
  87. /**
  88. * Load action type from database
  89. *
  90. * @param int|string $id id or code of action type to read
  91. * @return int 1=ok, 0=not found, -1=error
  92. */
  93. public function fetch($id)
  94. {
  95. $sql = "SELECT id, code, type, libelle as label, color, active, picto";
  96. $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
  97. if (is_numeric($id)) {
  98. $sql .= " WHERE id=".(int) $id;
  99. } else {
  100. $sql .= " WHERE code='".$this->db->escape($id)."'";
  101. }
  102. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  103. $resql = $this->db->query($sql);
  104. if ($resql) {
  105. if ($this->db->num_rows($resql)) {
  106. $obj = $this->db->fetch_object($resql);
  107. $this->id = $obj->id;
  108. $this->code = $obj->code;
  109. $this->type = $obj->type;
  110. $this->libelle = $obj->label; // deprecated
  111. $this->label = $obj->label;
  112. $this->active = $obj->active;
  113. $this->color = $obj->color;
  114. $this->db->free($resql);
  115. return 1;
  116. } else {
  117. $this->db->free($resql);
  118. return 0;
  119. }
  120. } else {
  121. $this->error = $this->db->error();
  122. return -1;
  123. }
  124. }
  125. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  126. /**
  127. * Return list of event types: array(id=>label) or array(code=>label)
  128. *
  129. * @param string|int $active 1 or 0 to filter on event state active or not ('' by default = no filter)
  130. * @param string $idorcode 'id' or 'code' or 'all'
  131. * @param string $excludetype Type to exclude ('system' or 'systemauto')
  132. * @param int $onlyautoornot 1=Group all type AC_XXX into 1 line AC_MANUAL. 0=Keep details of type, -1 or -2=Keep details and add a combined line per calendar (Default, Auto, BoothConf, ...)
  133. * @param string $morefilter Add more SQL filter
  134. * @param int $shortlabel 1=Get short label instead of long label
  135. * @return array|int Array of all event types if OK, <0 if KO. Key of array is id or code depending on parameter $idorcode.
  136. */
  137. public function liste_array($active = '', $idorcode = 'id', $excludetype = '', $onlyautoornot = 0, $morefilter = '', $shortlabel = 0)
  138. {
  139. // phpcs:enable
  140. global $langs, $conf, $user;
  141. $langs->load("commercial");
  142. $repid = array();
  143. $repcode = array();
  144. $repall = array();
  145. $sql = "SELECT id, code, libelle as label, module, type, color, picto";
  146. $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
  147. $sql .= " WHERE 1=1";
  148. if ($active != '') {
  149. $sql .= " AND active=".(int) $active;
  150. }
  151. if (!empty($excludetype)) {
  152. $sql .= " AND type <> '".$this->db->escape($excludetype)."'";
  153. }
  154. if ($morefilter) {
  155. $sql .= " AND ".$morefilter;
  156. }
  157. // If AGENDA_SORT_EVENT_TYPE_BY_POSITION_FIRST is defined, we use position as main sort criterion
  158. // otherwise we use type as main sort criterion
  159. if (getDolGlobalString('AGENDA_SORT_EVENT_TYPE_BY_POSITION_FIRST')) {
  160. $sql .= " ORDER BY position, type, module";
  161. } else {
  162. $sql .= " ORDER BY type, position, module";
  163. }
  164. dol_syslog(get_class($this)."::liste_array", LOG_DEBUG);
  165. $resql = $this->db->query($sql);
  166. if ($resql) {
  167. $nump = $this->db->num_rows($resql);
  168. if ($nump) {
  169. $idforallfornewmodule = 97;
  170. $i = 0;
  171. while ($i < $nump) {
  172. $obj = $this->db->fetch_object($resql);
  173. $qualified = 1;
  174. // $obj->type can be 'system', 'systemauto', 'module', 'moduleauto', 'xxx', 'xxxauto'
  175. // Note: type = system... than type of event is added among other standard events.
  176. // type = module... then type of event is grouped into module defined into module = myobject@mymodule. Example: Event organization or external modules
  177. // type = xxx... then type of event is added into list as a new flat value (not grouped). Example: Agefod external module
  178. if ($qualified && $onlyautoornot > 0 && preg_match('/^system/', $obj->type) && !preg_match('/^AC_OTH/', $obj->code)) {
  179. $qualified = 0; // We discard detailed system events. We keep only the 2 generic lines (AC_OTH and AC_OTH_AUTO)
  180. }
  181. if ($qualified && !empty($obj->module)) {
  182. //var_dump($obj->type.' '.$obj->module.' '); var_dump($user->hasRight('facture', 'lire'));
  183. $qualified = 0;
  184. // Special cases
  185. if ($obj->module == 'invoice' && isModEnabled('facture') && $user->hasRight('facture', 'lire')) {
  186. $qualified = 1;
  187. }
  188. if ($obj->module == 'order' && isModEnabled('commande') && !$user->hasRight('commande', 'lire')) {
  189. $qualified = 1;
  190. }
  191. if ($obj->module == 'propal' && isModEnabled("propal") && $user->hasRight('propal', 'lire')) {
  192. $qualified = 1;
  193. }
  194. if ($obj->module == 'invoice_supplier' && ((isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'facture', 'lire')) || (isModEnabled('supplier_invoice') && $user->hasRight('supplier_invoice', 'lire')))) {
  195. $qualified = 1;
  196. }
  197. if ($obj->module == 'order_supplier' && ((isModEnabled("fournisseur") && !getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight('fournisseur', 'commande', 'lire')) || (!isModEnabled('supplier_order') && $user->hasRight('supplier_order', 'lire')))) {
  198. $qualified = 1;
  199. }
  200. if ($obj->module == 'shipping' && isModEnabled("expedition") && $user->hasRight('expedition', 'lire')) {
  201. $qualified = 1;
  202. }
  203. // For case module = 'myobject@eventorganization'
  204. $tmparray = explode("@", $obj->module);
  205. if (count($tmparray) > 1 && $tmparray[1] == 'eventorganization' && isModEnabled('eventorganization')) {
  206. $qualified = 1;
  207. }
  208. // For the generic case with type = 'module...' and module = 'myobject@mymodule'
  209. $regs = array();
  210. if (preg_match('/^module/', $obj->type)) {
  211. if (preg_match('/^(.+)@(.+)$/', $obj->module, $regs)) {
  212. $tmpobject = $regs[1];
  213. $tmpmodule = $regs[2];
  214. //var_dump($user->$tmpmodule);
  215. if ($tmpmodule && isset($conf->$tmpmodule) && isModEnabled($tmpmodule) && ($user->hasRight($tmpmodule, 'read') || $user->hasRight($tmpmodule, 'lire') || $user->hasRight($tmpmodule, $tmpobject, 'read') || $user->hasRight($tmpmodule, $tmpobject, 'lire'))) {
  216. $qualified = 1;
  217. }
  218. }
  219. }
  220. // For the case type is not 'system...' neither 'module', we just check module is on
  221. if (! in_array($obj->type, array('system', 'systemauto', 'module', 'moduleauto'))) {
  222. $tmpmodule = $obj->module;
  223. //var_dump($tmpmodule);
  224. if ($tmpmodule && isset($conf->$tmpmodule) && isModEnabled($tmpmodule)) {
  225. $qualified = 1;
  226. }
  227. }
  228. }
  229. if ($qualified) {
  230. $keyfortrans = '';
  231. $transcode = '';
  232. $code = $obj->code;
  233. $typecalendar = $obj->type;
  234. if ($onlyautoornot > 0 && $typecalendar == 'system') {
  235. $code = 'AC_MANUAL';
  236. } elseif ($onlyautoornot > 0 && $typecalendar == 'systemauto') {
  237. $code = 'AC_AUTO';
  238. } elseif ($onlyautoornot > 0) {
  239. $code = 'AC_'.strtoupper($obj->module);
  240. }
  241. if ($shortlabel) {
  242. $keyfortrans = "Action".$code.'Short';
  243. $transcode = $langs->trans($keyfortrans);
  244. }
  245. if (empty($keyfortrans) || $keyfortrans == $transcode) {
  246. $keyfortrans = "Action".$code;
  247. $transcode = $langs->trans($keyfortrans);
  248. }
  249. $label = (($transcode != $keyfortrans) ? $transcode : $langs->trans($obj->label));
  250. if (($onlyautoornot == -1 || $onlyautoornot == -2) && getDolGlobalString('AGENDA_USE_EVENT_TYPE')) {
  251. if ($typecalendar == 'system') {
  252. $label = '&nbsp;&nbsp; '.$label;
  253. $repid[-99] = $langs->trans("ActionAC_MANUAL");
  254. $repcode['AC_NON_AUTO'] = '-- '.$langs->trans("ActionAC_MANUAL");
  255. }
  256. if ($typecalendar == 'systemauto') {
  257. $label = '&nbsp;&nbsp; '.$label;
  258. $repid[-98] = $langs->trans("ActionAC_AUTO");
  259. $repcode['AC_ALL_AUTO'] = '-- '.$langs->trans("ActionAC_AUTO");
  260. }
  261. if ($typecalendar == 'module') {
  262. //TODO check if possible to push it between system and systemauto
  263. if (preg_match('/@/', $obj->module)) {
  264. $module = explode('@', $obj->module)[1];
  265. } else {
  266. $module = $obj->module;
  267. }
  268. $label = '&nbsp;&nbsp; '.$label;
  269. if (!isset($repcode['AC_ALL_'.strtoupper($module)])) { // If first time for this module
  270. $idforallfornewmodule--;
  271. }
  272. $repid[$idforallfornewmodule] = $langs->trans("ActionAC_ALL_".strtoupper($module));
  273. $repcode['AC_ALL_'.strtoupper($module)] = '-- '.$langs->trans("Module").' '.ucfirst($module);
  274. }
  275. }
  276. $repid[$obj->id] = $label;
  277. $repcode[$obj->code] = $label;
  278. $repall[$obj->code] = array('id' => $label, 'label' => $label, 'type' => $typecalendar, 'color' => $obj->color, 'picto' => $obj->picto);
  279. if ($onlyautoornot > 0 && preg_match('/^module/', $obj->type) && $obj->module) {
  280. $repcode[$obj->code] .= ' ('.$langs->trans("Module").': '.$obj->module.')';
  281. $repall[$obj->code]['label'] .= ' ('.$langs->trans("Module").': '.$obj->module.')';
  282. }
  283. }
  284. $i++;
  285. }
  286. }
  287. if ($idorcode == 'id') {
  288. $this->liste_array = $repid;
  289. } elseif ($idorcode == 'code') {
  290. $this->liste_array = $repcode;
  291. } else {
  292. $this->liste_array = $repall;
  293. }
  294. return $this->liste_array;
  295. } else {
  296. $this->error = $this->db->lasterror();
  297. return -1;
  298. }
  299. }
  300. /**
  301. * Return name of action type as a label translated
  302. *
  303. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Picto only
  304. * @return string Label of action type
  305. */
  306. public function getNomUrl($withpicto = 0)
  307. {
  308. global $langs;
  309. // Check if translation available
  310. $transcode = $langs->trans("Action".$this->code);
  311. if ($transcode != "Action".$this->code) {
  312. return $transcode;
  313. }
  314. return -1;
  315. }
  316. }