cactioncomm.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. * Constructor
  76. *
  77. * @param DoliDB $db Database handler
  78. */
  79. public function __construct($db)
  80. {
  81. $this->db = $db;
  82. }
  83. /**
  84. * Load action type from database
  85. *
  86. * @param int|string $id id or code of action type to read
  87. * @return int 1=ok, 0=not found, -1=error
  88. */
  89. public function fetch($id)
  90. {
  91. $sql = "SELECT id, code, type, libelle as label, color, active, picto";
  92. $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
  93. if (is_numeric($id)) {
  94. $sql .= " WHERE id=".(int) $id;
  95. } else {
  96. $sql .= " WHERE code='".$this->db->escape($id)."'";
  97. }
  98. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  99. $resql = $this->db->query($sql);
  100. if ($resql) {
  101. if ($this->db->num_rows($resql)) {
  102. $obj = $this->db->fetch_object($resql);
  103. $this->id = $obj->id;
  104. $this->code = $obj->code;
  105. $this->type = $obj->type;
  106. $this->libelle = $obj->label; // deprecated
  107. $this->label = $obj->label;
  108. $this->active = $obj->active;
  109. $this->color = $obj->color;
  110. $this->db->free($resql);
  111. return 1;
  112. } else {
  113. $this->db->free($resql);
  114. return 0;
  115. }
  116. } else {
  117. $this->error = $this->db->error();
  118. return -1;
  119. }
  120. }
  121. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  122. /**
  123. * Return list of event types: array(id=>label) or array(code=>label)
  124. *
  125. * @param string|int $active 1 or 0 to filter on event state active or not ('' by default = no filter)
  126. * @param string $idorcode 'id' or 'code' or 'all'
  127. * @param string $excludetype Type to exclude ('system' or 'systemauto')
  128. * @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, ...)
  129. * @param string $morefilter Add more SQL filter
  130. * @param int $shortlabel 1=Get short label instead of long label
  131. * @return mixed Array of all event types if OK, <0 if KO. Key of array is id or code depending on parameter $idorcode.
  132. */
  133. public function liste_array($active = '', $idorcode = 'id', $excludetype = '', $onlyautoornot = 0, $morefilter = '', $shortlabel = 0)
  134. {
  135. // phpcs:enable
  136. global $langs, $conf, $user;
  137. $langs->load("commercial");
  138. $repid = array();
  139. $repcode = array();
  140. $repall = array();
  141. $sql = "SELECT id, code, libelle as label, module, type, color, picto";
  142. $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm";
  143. $sql .= " WHERE 1=1";
  144. if ($active != '') {
  145. $sql .= " AND active=".(int) $active;
  146. }
  147. if (!empty($excludetype)) {
  148. $sql .= " AND type <> '".$this->db->escape($excludetype)."'";
  149. }
  150. if ($morefilter) {
  151. $sql .= " AND ".$morefilter;
  152. }
  153. $sql .= " ORDER BY type, position, module";
  154. dol_syslog(get_class($this)."::liste_array", LOG_DEBUG);
  155. $resql = $this->db->query($sql);
  156. if ($resql) {
  157. $nump = $this->db->num_rows($resql);
  158. if ($nump) {
  159. $idforallfornewmodule = 97;
  160. $i = 0;
  161. while ($i < $nump) {
  162. $obj = $this->db->fetch_object($resql);
  163. $qualified = 1;
  164. // $obj->type can be 'system', 'systemauto', 'module', 'moduleauto', 'xxx', 'xxxauto'
  165. // Note: type = system... than type of event is added among other standard events.
  166. // type = module... then type of event is grouped into module defined into module = myobject@mymodule. Example: Event organization or external modules
  167. // type = xxx... then type of event is added into list as a new flat value (not grouped). Example: Agefod external module
  168. if ($qualified && $onlyautoornot > 0 && preg_match('/^system/', $obj->type) && !preg_match('/^AC_OTH/', $obj->code)) {
  169. $qualified = 0; // We discard detailed system events. We keep only the 2 generic lines (AC_OTH and AC_OTH_AUTO)
  170. }
  171. if ($qualified && !empty($obj->module)) {
  172. //var_dump($obj->type.' '.$obj->module.' '); var_dump($user->rights->facture->lire);
  173. $qualified = 0;
  174. // Special cases
  175. if ($obj->module == 'invoice' && isModEnabled('facture') && !empty($user->rights->facture->lire)) {
  176. $qualified = 1;
  177. }
  178. if ($obj->module == 'order' && isModEnabled('commande') && empty($user->rights->commande->lire)) {
  179. $qualified = 1;
  180. }
  181. if ($obj->module == 'propal' && isModEnabled("propal") && !empty($user->rights->propal->lire)) {
  182. $qualified = 1;
  183. }
  184. if ($obj->module == 'invoice_supplier' && ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && !empty($user->rights->fournisseur->facture->lire)) || (isModEnabled('supplier_invoice') && !empty($user->rights->supplier_invoice->lire)))) {
  185. $qualified = 1;
  186. }
  187. if ($obj->module == 'order_supplier' && ((isModEnabled("fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && !empty($user->rights->fournisseur->commande->lire)) || (!isModEnabled('supplier_order') && !empty($user->rights->supplier_order->lire)))) {
  188. $qualified = 1;
  189. }
  190. if ($obj->module == 'shipping' && isModEnabled("expedition") && !empty($user->rights->expedition->lire)) {
  191. $qualified = 1;
  192. }
  193. // For case module = 'myobject@eventorganization'
  194. $tmparray = preg_split("/@/", $obj->module, -1);
  195. if (count($tmparray) > 1 && $tmparray[1] == 'eventorganization' && isModEnabled('eventorganization')) {
  196. $qualified = 1;
  197. }
  198. // For the generic case with type = 'module...' and module = 'myobject@mymodule'
  199. $regs = array();
  200. if (preg_match('/^module/', $obj->type)) {
  201. if (preg_match('/^(.+)@(.+)$/', $obj->module, $regs)) {
  202. $tmpobject = $regs[1];
  203. $tmpmodule = $regs[2];
  204. //var_dump($user->$tmpmodule);
  205. if ($tmpmodule && isset($conf->$tmpmodule) && isModEnabled($tmpmodule) && (!empty($user->rights->$tmpmodule->read) || !empty($user->rights->$tmpmodule->lire) || !empty($user->rights->$tmpmodule->$tmpobject->read) || !empty($user->rights->$tmpmodule->$tmpobject->lire))) {
  206. $qualified = 1;
  207. }
  208. }
  209. }
  210. // For the case type is not 'system...' neither 'module', we just check module is on
  211. if (! in_array($obj->type, array('system', 'systemauto', 'module', 'moduleauto'))) {
  212. $tmpmodule = $obj->module;
  213. //var_dump($tmpmodule);
  214. if ($tmpmodule && isset($conf->$tmpmodule) && isModEnabled($tmpmodule)) {
  215. $qualified = 1;
  216. }
  217. }
  218. }
  219. if ($qualified) {
  220. $keyfortrans = '';
  221. $transcode = '';
  222. $code = $obj->code;
  223. $typecalendar = $obj->type;
  224. if ($onlyautoornot > 0 && $typecalendar == 'system') {
  225. $code = 'AC_MANUAL';
  226. } elseif ($onlyautoornot > 0 && $typecalendar == 'systemauto') {
  227. $code = 'AC_AUTO';
  228. } elseif ($onlyautoornot > 0) {
  229. $code = 'AC_'.strtoupper($obj->module);
  230. }
  231. if ($shortlabel) {
  232. $keyfortrans = "Action".$code.'Short';
  233. $transcode = $langs->trans($keyfortrans);
  234. }
  235. if (empty($keyfortrans) || $keyfortrans == $transcode) {
  236. $keyfortrans = "Action".$code;
  237. $transcode = $langs->trans($keyfortrans);
  238. }
  239. $label = (($transcode != $keyfortrans) ? $transcode : $langs->trans($obj->label));
  240. if (($onlyautoornot == -1 || $onlyautoornot == -2) && !empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
  241. if ($typecalendar == 'system') {
  242. $label = '&nbsp;&nbsp; '.$label;
  243. $repid[-99] = $langs->trans("ActionAC_MANUAL");
  244. $repcode['AC_NON_AUTO'] = '-- '.$langs->trans("ActionAC_MANUAL");
  245. }
  246. if ($typecalendar == 'systemauto') {
  247. $label = '&nbsp;&nbsp; '.$label;
  248. $repid[-98] = $langs->trans("ActionAC_AUTO");
  249. $repcode['AC_ALL_AUTO'] = '-- '.$langs->trans("ActionAC_AUTO");
  250. }
  251. if ($typecalendar == 'module') {
  252. //TODO check if possible to push it between system and systemauto
  253. if (preg_match('/@/', $obj->module)) {
  254. $module = explode('@', $obj->module)[1];
  255. } else {
  256. $module = $obj->module;
  257. }
  258. $label = '&nbsp;&nbsp; '.$label;
  259. if (!isset($repcode['AC_ALL_'.strtoupper($module)])) { // If first time for this module
  260. $idforallfornewmodule--;
  261. }
  262. $repid[$idforallfornewmodule] = $langs->trans("ActionAC_ALL_".strtoupper($module));
  263. $repcode['AC_ALL_'.strtoupper($module)] = '-- '.$langs->trans("Module").' '.ucfirst($module);
  264. }
  265. }
  266. $repid[$obj->id] = $label;
  267. $repcode[$obj->code] = $label;
  268. $repall[$obj->code] = array('id' => $label, 'label' => $label, 'type' => $typecalendar, 'color' => $obj->color, 'picto' => $obj->picto);
  269. if ($onlyautoornot > 0 && preg_match('/^module/', $obj->type) && $obj->module) {
  270. $repcode[$obj->code] .= ' ('.$langs->trans("Module").': '.$obj->module.')';
  271. $repall[$obj->code]['label'] .= ' ('.$langs->trans("Module").': '.$obj->module.')';
  272. }
  273. }
  274. $i++;
  275. }
  276. }
  277. if ($idorcode == 'id') {
  278. $this->liste_array = $repid;
  279. } elseif ($idorcode == 'code') {
  280. $this->liste_array = $repcode;
  281. } else {
  282. $this->liste_array = $repall;
  283. }
  284. return $this->liste_array;
  285. } else {
  286. $this->error = $this->db->lasterror();
  287. return -1;
  288. }
  289. }
  290. /**
  291. * Return name of action type as a label translated
  292. *
  293. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Picto only
  294. * @return string Label of action type
  295. */
  296. public function getNomUrl($withpicto = 0)
  297. {
  298. global $langs;
  299. // Check if translation available
  300. $transcode = $langs->trans("Action".$this->code);
  301. if ($transcode != "Action".$this->code) {
  302. return $transcode;
  303. }
  304. }
  305. }