coreobject.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /* EXPERIMENTAL
  3. *
  4. * Copyright (C) 2016 ATM Consulting <support@atm-consulting.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 htdocs/core/class/coreobject.class.php
  21. * \ingroup core
  22. * \brief File of class to manage all object. Might be replace or merge into commonobject
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  25. // TODO Remove this class (used in Expensereportik and ExpenseReportRule
  26. /**
  27. * CoreObject
  28. */
  29. class CoreObject extends CommonObject
  30. {
  31. public $withChild = true;
  32. /**
  33. * @var Array $_fields Fields to synchronize with Database
  34. */
  35. protected $fields = array();
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. */
  41. public function __construct(DoliDB &$db)
  42. {
  43. $this->db = $db;
  44. }
  45. /**
  46. * Function to init fields
  47. *
  48. * @return bool
  49. */
  50. protected function init()
  51. {
  52. $this->id = 0;
  53. $this->datec = 0;
  54. $this->tms = 0;
  55. if (!empty($this->fields)) {
  56. foreach ($this->fields as $field => $info) {
  57. if ($this->isDate($info)) {
  58. $this->{$field} = time();
  59. } elseif ($this->isArray($info)) {
  60. $this->{$field} = array();
  61. } elseif ($this->isInt($info)) {
  62. $this->{$field} = (int) 0;
  63. } elseif ($this->isFloat($info)) {
  64. $this->{$field} = (double) 0;
  65. } else {
  66. $this->{$field} = '';
  67. }
  68. }
  69. $this->to_delete = false;
  70. $this->is_clone = false;
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. /**
  77. * Test type of field
  78. *
  79. * @param string $field name of field
  80. * @param string $type type of field to test
  81. * @return boolean value of field or false
  82. */
  83. private function checkFieldType($field, $type)
  84. {
  85. if (isset($this->fields[$field]) && method_exists($this, 'is_'.$type)) {
  86. return $this->{'is_'.$type}($this->fields[$field]);
  87. } else {
  88. return false;
  89. }
  90. }
  91. /**
  92. * Get object and children from database
  93. *
  94. * @param int $id Id of object to load
  95. * @param bool $loadChild used to load children from database
  96. * @return int >0 if OK, <0 if KO, 0 if not found
  97. */
  98. public function fetch($id, $loadChild = true)
  99. {
  100. $res = $this->fetchCommon($id);
  101. if ($res > 0) {
  102. if ($loadChild) {
  103. $this->fetchChild();
  104. }
  105. }
  106. return $res;
  107. }
  108. /**
  109. * Function to instantiate a new child
  110. *
  111. * @param string $tabName Table name of child
  112. * @param int $id If id is given, we try to return his key if exist or load if we try_to_load
  113. * @param string $key Attribute name of the object id
  114. * @param bool $try_to_load Force the fetch if an id is given
  115. * @return int
  116. */
  117. public function addChild($tabName, $id = 0, $key = 'id', $try_to_load = false)
  118. {
  119. if (!empty($id)) {
  120. foreach ($this->{$tabName} as $k => &$object) {
  121. if ($object->{$key} === $id) {
  122. return $k;
  123. }
  124. }
  125. }
  126. $k = count($this->{$tabName});
  127. $className = ucfirst($tabName);
  128. $this->{$tabName}[$k] = new $className($this->db);
  129. if ($id > 0 && $key === 'id' && $try_to_load) {
  130. $this->{$tabName}[$k]->fetch($id);
  131. }
  132. return $k;
  133. }
  134. /**
  135. * Function to set a child as to delete
  136. *
  137. * @param string $tabName Table name of child
  138. * @param int $id Id of child to set as to delete
  139. * @param string $key Attribute name of the object id
  140. * @return bool
  141. */
  142. public function removeChild($tabName, $id, $key = 'id')
  143. {
  144. foreach ($this->{$tabName} as &$object) {
  145. if ($object->{$key} == $id) {
  146. $object->to_delete = true;
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. /**
  153. * Function to fetch children objects
  154. *
  155. * @return void
  156. */
  157. public function fetchChild()
  158. {
  159. if ($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) {
  160. foreach ($this->childtables as &$childTable) {
  161. $className = ucfirst($childTable);
  162. $this->{$className} = array();
  163. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$childTable." WHERE ".$this->fk_element." = ".((int) $this->id);
  164. $res = $this->db->query($sql);
  165. if ($res) {
  166. while ($obj = $this->db->fetch_object($res)) {
  167. $o = new $className($this->db);
  168. $o->fetch($obj->rowid);
  169. $this->{$className}[] = $o;
  170. }
  171. } else {
  172. $this->errors[] = $this->db->lasterror();
  173. }
  174. }
  175. }
  176. }
  177. /**
  178. * Function to update children data
  179. *
  180. * @param User $user user object
  181. * @return void
  182. */
  183. public function saveChild(User &$user)
  184. {
  185. if ($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) {
  186. foreach ($this->childtables as &$childTable) {
  187. $className = ucfirst($childTable);
  188. if (!empty($this->{$className})) {
  189. foreach ($this->{$className} as $i => &$object) {
  190. $object->{$this->fk_element} = $this->id;
  191. $object->update($user);
  192. if ($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete == true) {
  193. unset($this->{$className}[$i]);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * Function to update object or create or delete if needed
  202. *
  203. * @param User $user User object
  204. * @return int < 0 if KO, > 0 if OK
  205. */
  206. public function update(User &$user)
  207. {
  208. if (empty($this->id)) {
  209. return $this->create($user); // To test, with that, no need to test on high level object, the core decide it, update just needed
  210. } elseif (isset($this->to_delete) && $this->to_delete == true) {
  211. return $this->delete($user);
  212. }
  213. $error = 0;
  214. $this->db->begin();
  215. $res = $this->updateCommon($user);
  216. if ($res) {
  217. $result = $this->call_trigger(strtoupper($this->element).'_UPDATE', $user);
  218. if ($result < 0) {
  219. $error++;
  220. } else {
  221. $this->saveChild($user);
  222. }
  223. } else {
  224. $error++;
  225. $this->error = $this->db->lasterror();
  226. $this->errors[] = $this->error;
  227. }
  228. if (empty($error)) {
  229. $this->db->commit();
  230. return $this->id;
  231. } else {
  232. $this->db->rollback();
  233. return -1;
  234. }
  235. }
  236. /**
  237. * Function to create object in database
  238. *
  239. * @param User $user User object
  240. * @return int < 0 if KO, > 0 if OK
  241. */
  242. public function create(User $user)
  243. {
  244. if ($this->id > 0) {
  245. return $this->update($user);
  246. }
  247. $error = 0;
  248. $this->db->begin();
  249. $res = $this->createCommon($user);
  250. if ($res) {
  251. $this->id = $this->db->last_insert_id($this->table_element);
  252. $result = $this->call_trigger(strtoupper($this->element).'_CREATE', $user);
  253. if ($result < 0) {
  254. $error++;
  255. } else {
  256. $this->saveChild($user);
  257. }
  258. } else {
  259. $error++;
  260. $this->error = $this->db->lasterror();
  261. $this->errors[] = $this->error;
  262. }
  263. if (empty($error)) {
  264. $this->db->commit();
  265. return $this->id;
  266. } else {
  267. $this->db->rollback();
  268. return -1;
  269. }
  270. }
  271. /**
  272. * Function to delete object in database
  273. *
  274. * @param User $user user object
  275. * @return int < 0 if KO, > 0 if OK
  276. */
  277. public function delete(User &$user)
  278. {
  279. if ($this->id <= 0) {
  280. return 0;
  281. }
  282. $error = 0;
  283. $this->db->begin();
  284. $result = $this->call_trigger(strtoupper($this->element).'_DELETE', $user);
  285. if ($result < 0) {
  286. $error++;
  287. }
  288. if (!$error) {
  289. $this->deleteCommon($user);
  290. if ($this->withChild && !empty($this->childtables)) {
  291. foreach ($this->childtables as &$childTable) {
  292. $className = ucfirst($childTable);
  293. if (!empty($this->{$className})) {
  294. foreach ($this->{$className} as &$object) {
  295. $object->delete($user);
  296. }
  297. }
  298. }
  299. }
  300. }
  301. if (empty($error)) {
  302. $this->db->commit();
  303. return 1;
  304. } else {
  305. $this->error = $this->db->lasterror();
  306. $this->errors[] = $this->error;
  307. $this->db->rollback();
  308. return -1;
  309. }
  310. }
  311. /**
  312. * Function to get a formatted date
  313. *
  314. * @param string $field Attribute to return
  315. * @param string $format Output date format
  316. * @return string
  317. */
  318. public function getDate($field, $format = '')
  319. {
  320. if (empty($this->{$field})) {
  321. return '';
  322. } else {
  323. return dol_print_date($this->{$field}, $format);
  324. }
  325. }
  326. /**
  327. * Function to set date in field
  328. *
  329. * @param string $field field to set
  330. * @param string $date formatted date to convert
  331. * @return mixed
  332. */
  333. public function setDate($field, $date)
  334. {
  335. if (empty($date)) {
  336. $this->{$field} = 0;
  337. } else {
  338. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  339. $this->{$field} = dol_stringtotime($date);
  340. }
  341. return $this->{$field};
  342. }
  343. /**
  344. * Function to update current object
  345. *
  346. * @param array $Tab Array of values
  347. * @return int
  348. */
  349. public function setValues(&$Tab)
  350. {
  351. foreach ($Tab as $key => $value) {
  352. if ($this->checkFieldType($key, 'date')) {
  353. $this->setDate($key, $value);
  354. } elseif ($this->checkFieldType($key, 'float')) {
  355. $this->{$key} = (double) price2num($value);
  356. } elseif ($this->checkFieldType($key, 'int')) {
  357. $this->{$key} = (int) price2num($value);
  358. } else {
  359. $this->{$key} = dol_string_nohtmltag($value);
  360. }
  361. }
  362. return 1;
  363. }
  364. }