ProductAttribute.class.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. <?php
  2. /* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  3. * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
  4. * Copyright (C) 2023 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. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  20. /**
  21. * Class ProductAttribute
  22. * Used to represent a product attribute
  23. */
  24. class ProductAttribute extends CommonObject
  25. {
  26. /**
  27. * Database handler
  28. * @var DoliDB
  29. */
  30. public $db;
  31. /**
  32. * @var string ID of module.
  33. */
  34. public $module = 'variants';
  35. /**
  36. * @var string ID to identify managed object.
  37. */
  38. public $element = 'productattribute';
  39. /**
  40. * @var string Name of table without prefix where object is stored. This is also the key used for extrafields management.
  41. */
  42. public $table_element = 'product_attribute';
  43. /**
  44. * @var string Name of sub table line
  45. */
  46. public $table_element_line = 'product_attribute_value';
  47. /**
  48. * @var string Field with ID of parent key if this field has a parent or for child tables
  49. */
  50. public $fk_element = 'fk_product_attribute';
  51. /**
  52. * @var int Does this object support multicompany module ?
  53. * 0=No test on entity, 1=Test with field entity, 'field@table'=Test with link by field@table
  54. */
  55. public $ismultientitymanaged = 1;
  56. /**
  57. * @var int Does object support extrafields ? 0=No, 1=Yes
  58. */
  59. public $isextrafieldmanaged = 0;
  60. /**
  61. * @var string String with name of icon for conferenceorbooth. Must be the part after the 'object_' into object_conferenceorbooth.png
  62. */
  63. public $picto = 'product';
  64. /**
  65. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  66. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  67. * 'label' the translation key.
  68. * 'picto' is code of a picto to show before value in forms
  69. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  70. * 'position' is the sort order of field.
  71. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  72. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  73. * 'noteditable' says if field is not editable (1 or 0)
  74. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  75. * 'index' if we want an index in database.
  76. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  77. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  78. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  79. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200'
  80. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  81. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  82. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  83. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  84. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  85. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  86. *
  87. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  88. */
  89. /**
  90. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  91. */
  92. public $fields=array(
  93. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
  94. 'ref' => array('type'=>'varchar(255)', 'label'=>'Ref', 'visible'=>1, 'enabled'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'css'=>''),
  95. 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'ExternalRef', 'enabled' => 1, 'visible' => 0, 'position' => 20, 'searchall'=>1),
  96. 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>'1', 'position'=>30, 'notnull'=>1, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'help'=>"", 'showoncombobox'=>'1',),
  97. 'position' => array('type'=>'integer', 'label'=>'Rank', 'enabled'=>1, 'visible'=>0, 'default'=>0, 'position'=>40, 'notnull'=>1,),
  98. );
  99. /**
  100. * @var int rowid
  101. */
  102. public $id;
  103. /**
  104. * @var string ref
  105. */
  106. public $ref;
  107. /**
  108. * @var string external ref
  109. */
  110. public $ref_ext;
  111. /**
  112. * @var string label
  113. */
  114. public $label;
  115. /**
  116. * @var int position
  117. * @deprecated
  118. * @see $position
  119. */
  120. public $rang;
  121. /**
  122. * @var int position
  123. */
  124. public $position;
  125. /**
  126. * @var ProductAttributeValue[]
  127. */
  128. public $lines = array();
  129. /**
  130. * @var ProductAttributeValue
  131. */
  132. public $line;
  133. /**
  134. * @var int Number of product that use this attribute
  135. */
  136. public $is_used_by_products;
  137. /**
  138. * Constructor
  139. *
  140. * @param DoliDb $db Database handler
  141. */
  142. public function __construct(DoliDB $db)
  143. {
  144. global $conf, $langs;
  145. $this->db = $db;
  146. $this->entity = $conf->entity;
  147. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
  148. $this->fields['rowid']['visible'] = 0;
  149. }
  150. if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
  151. $this->fields['entity']['enabled'] = 0;
  152. }
  153. // Unset fields that are disabled
  154. foreach ($this->fields as $key => $val) {
  155. if (isset($val['enabled']) && empty($val['enabled'])) {
  156. unset($this->fields[$key]);
  157. }
  158. }
  159. // Translate some data of arrayofkeyval
  160. if (is_object($langs)) {
  161. foreach ($this->fields as $key => $val) {
  162. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  163. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  164. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * Creates a product attribute
  172. *
  173. * @param User $user Object user
  174. * @param int $notrigger Do not execute trigger
  175. * @return int <0 KO, Id of new variant if OK
  176. */
  177. public function create(User $user, $notrigger = 0)
  178. {
  179. global $langs;
  180. $error = 0;
  181. // Clean parameters
  182. $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
  183. $this->label = trim($this->label);
  184. $this->position = $this->position > 0 ? $this->position : 0;
  185. // Position to use
  186. if (empty($this->position)) {
  187. $positionmax = $this->getMaxAttributesPosition();
  188. $this->position = $positionmax + 1;
  189. }
  190. // Check parameters
  191. if (empty($this->ref)) {
  192. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
  193. $error++;
  194. }
  195. if (empty($this->label)) {
  196. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label"));
  197. $error++;
  198. }
  199. if ($error) {
  200. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  201. return -1;
  202. }
  203. $this->db->begin();
  204. $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (";
  205. $sql .= " ref, ref_ext, label, entity, position";
  206. $sql .= ")";
  207. $sql .= " VALUES (";
  208. $sql .= " '" . $this->db->escape($this->ref) . "'";
  209. $sql .= ", '" . $this->db->escape($this->ref_ext) . "'";
  210. $sql .= ", '" . $this->db->escape($this->label) . "'";
  211. $sql .= ", " . ((int) $this->entity);
  212. $sql .= ", " . ((int) $this->position);
  213. $sql .= ")";
  214. dol_syslog(__METHOD__, LOG_DEBUG);
  215. $resql = $this->db->query($sql);
  216. if (!$resql) {
  217. $this->errors[] = "Error " . $this->db->lasterror();
  218. $error++;
  219. }
  220. if (!$error) {
  221. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  222. }
  223. if (!$error && !$notrigger) {
  224. // Call trigger
  225. $result = $this->call_trigger('PRODUCT_ATTRIBUTE_CREATE', $user);
  226. if ($result < 0) {
  227. $error++;
  228. }
  229. // End call triggers
  230. }
  231. if (!$error) {
  232. $this->db->commit();
  233. return $this->id;
  234. } else {
  235. $this->db->rollback();
  236. return -1 * $error;
  237. }
  238. }
  239. /**
  240. * Fetches the properties of a product attribute
  241. *
  242. * @param int $id Attribute id
  243. * @return int <1 KO, >1 OK
  244. */
  245. public function fetch($id)
  246. {
  247. global $langs;
  248. $error = 0;
  249. // Clean parameters
  250. $id = $id > 0 ? $id : 0;
  251. // Check parameters
  252. if (empty($id)) {
  253. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  254. $error++;
  255. }
  256. if ($error) {
  257. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  258. return -1;
  259. }
  260. $sql = "SELECT rowid, ref, ref_ext, label, position";
  261. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  262. $sql .= " WHERE rowid = " . ((int) $id);
  263. $sql .= " AND entity IN (" . getEntity('product') . ")";
  264. dol_syslog(__METHOD__, LOG_DEBUG);
  265. $resql = $this->db->query($sql);
  266. if (!$resql) {
  267. $this->errors[] = "Error " . $this->db->lasterror();
  268. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  269. return -1;
  270. }
  271. $numrows = $this->db->num_rows($resql);
  272. if ($numrows) {
  273. $obj = $this->db->fetch_object($resql);
  274. $this->id = $obj->rowid;
  275. $this->ref = $obj->ref;
  276. $this->ref_ext = $obj->ref_ext;
  277. $this->label = $obj->label;
  278. $this->rang = $obj->position; // deprecated
  279. $this->position = $obj->position;
  280. }
  281. $this->db->free($resql);
  282. return $numrows;
  283. }
  284. /**
  285. * Returns an array of all product variants
  286. *
  287. * @return ProductAttribute[]
  288. */
  289. public function fetchAll()
  290. {
  291. $return = array();
  292. $sql = "SELECT rowid, ref, ref_ext, label, position";
  293. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element;
  294. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  295. $sql .= $this->db->order("position", "asc");
  296. dol_syslog(__METHOD__, LOG_DEBUG);
  297. $resql = $this->db->query($sql);
  298. if (!$resql) {
  299. $this->errors[] = "Error " . $this->db->lasterror();
  300. dol_print_error($this->db);
  301. return $return;
  302. }
  303. while ($obj = $this->db->fetch_object($resql)) {
  304. $tmp = new ProductAttribute($this->db);
  305. $tmp->id = $obj->rowid;
  306. $tmp->ref = $obj->ref;
  307. $tmp->ref_ext = $obj->ref_ext;
  308. $tmp->label = $obj->label;
  309. $tmp->rang = $obj->position; // deprecated
  310. $tmp->position = $obj->position;
  311. $return[] = $tmp;
  312. }
  313. return $return;
  314. }
  315. /**
  316. * Updates a product attribute
  317. *
  318. * @param User $user Object user
  319. * @param int $notrigger Do not execute trigger
  320. * @return int <0 KO, >0 OK
  321. */
  322. public function update(User $user, $notrigger = 0)
  323. {
  324. global $langs;
  325. $error = 0;
  326. // Clean parameters
  327. $this->id = $this->id > 0 ? $this->id : 0;
  328. $this->ref = strtoupper(dol_sanitizeFileName(dol_string_nospecial(trim($this->ref)))); // Ref must be uppercase
  329. $this->label = trim($this->label);
  330. // Check parameters
  331. if (empty($this->id)) {
  332. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  333. $error++;
  334. }
  335. if (empty($this->ref)) {
  336. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref"));
  337. $error++;
  338. }
  339. if (empty($this->label)) {
  340. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label"));
  341. $error++;
  342. }
  343. if ($error) {
  344. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  345. return -1;
  346. }
  347. $this->db->begin();
  348. $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET";
  349. $sql .= " ref = '" . $this->db->escape($this->ref) . "'";
  350. $sql .= ", ref_ext = '" . $this->db->escape($this->ref_ext) . "'";
  351. $sql .= ", label = '" . $this->db->escape($this->label) . "'";
  352. $sql .= ", position = " . ((int) $this->position);
  353. $sql .= " WHERE rowid = " . ((int) $this->id);
  354. dol_syslog(__METHOD__, LOG_DEBUG);
  355. $resql = $this->db->query($sql);
  356. if (!$resql) {
  357. $this->errors[] = "Error " . $this->db->lasterror();
  358. $error++;
  359. }
  360. if (!$error && !$notrigger) {
  361. // Call trigger
  362. $result = $this->call_trigger('PRODUCT_ATTRIBUTE_MODIFY', $user);
  363. if ($result < 0) {
  364. $error++;
  365. }
  366. // End call triggers
  367. }
  368. if (!$error) {
  369. $this->db->commit();
  370. return 1;
  371. } else {
  372. $this->db->rollback();
  373. return -1 * $error;
  374. }
  375. }
  376. /**
  377. * Deletes a product attribute
  378. *
  379. * @param User $user Object user
  380. * @param int $notrigger Do not execute trigger
  381. * @return int <0 KO, >0 OK
  382. */
  383. public function delete(User $user, $notrigger = 0)
  384. {
  385. global $langs;
  386. $error = 0;
  387. // Clean parameters
  388. $this->id = $this->id > 0 ? $this->id : 0;
  389. // Check parameters
  390. if (empty($this->id)) {
  391. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  392. $error++;
  393. }
  394. if ($error) {
  395. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  396. return -1;
  397. }
  398. $result = $this->isUsed();
  399. if ($result < 0) {
  400. return -1;
  401. } elseif ($result > 0) {
  402. $this->errors[] = $langs->trans('ErrorAttributeIsUsedIntoProduct');
  403. return -1;
  404. }
  405. $this->db->begin();
  406. if (!$notrigger) {
  407. // Call trigger
  408. $result = $this->call_trigger('PRODUCT_ATTRIBUTE_DELETE', $user);
  409. if ($result < 0) {
  410. $error++;
  411. }
  412. // End call triggers
  413. }
  414. if (!$error) {
  415. // Delete values
  416. $sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element_line;
  417. $sql .= " WHERE " . $this->fk_element . " = " . ((int) $this->id);
  418. dol_syslog(__METHOD__ . ' - Delete values', LOG_DEBUG);
  419. $resql = $this->db->query($sql);
  420. if (!$resql) {
  421. $this->errors[] = "Error " . $this->db->lasterror();
  422. $error++;
  423. }
  424. }
  425. if (!$error) {
  426. $sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element;
  427. $sql .= " WHERE rowid = " . ((int) $this->id);
  428. dol_syslog(__METHOD__ . ' - Delete attribute', LOG_DEBUG);
  429. $resql = $this->db->query($sql);
  430. if (!$resql) {
  431. $this->errors[] = "Error " . $this->db->lasterror();
  432. $error++;
  433. }
  434. }
  435. if (!$error) {
  436. $this->db->commit();
  437. return 1;
  438. } else {
  439. $this->db->rollback();
  440. return -1 * $error;
  441. }
  442. }
  443. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  444. /**
  445. * Load array lines
  446. *
  447. * @param string $filters Filter on other fields
  448. * @return int <0 if KO, >0 if OK
  449. */
  450. public function fetch_lines($filters = '')
  451. {
  452. // phpcs:enable
  453. global $langs;
  454. $this->lines = array();
  455. $error = 0;
  456. // Clean parameters
  457. $this->id = $this->id > 0 ? $this->id : 0;
  458. // Check parameters
  459. if (empty($this->id)) {
  460. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  461. $error++;
  462. }
  463. if ($error) {
  464. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  465. return -1;
  466. }
  467. $sql = "SELECT td.rowid, td.fk_product_attribute, td.ref, td.value, td.position";
  468. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element_line . " AS td";
  469. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $this->table_element . " AS t ON t.rowid = td." . $this->fk_element;
  470. $sql .= " WHERE t.rowid = " . ((int) $this->id);
  471. $sql .= " AND t.entity IN (" . getEntity('product') . ")";
  472. if ($filters) {
  473. $sql .= $filters;
  474. }
  475. $sql .= $this->db->order("td.position", "asc");
  476. dol_syslog(__METHOD__, LOG_DEBUG);
  477. $resql = $this->db->query($sql);
  478. if (!$resql) {
  479. $this->errors[] = "Error " . $this->db->lasterror();
  480. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  481. return -3;
  482. }
  483. $num = $this->db->num_rows($resql);
  484. if ($num) {
  485. $i = 0;
  486. while ($i < $num) {
  487. $obj = $this->db->fetch_object($resql);
  488. $line = new ProductAttributeValue($this->db);
  489. $line->id = $obj->rowid;
  490. $line->fk_product_attribute = $obj->fk_product_attribute;
  491. $line->ref = $obj->ref;
  492. $line->value = $obj->value;
  493. $line->position = $obj->position;
  494. $this->lines[$i] = $line;
  495. $i++;
  496. }
  497. }
  498. $this->db->free($resql);
  499. return $num;
  500. }
  501. /**
  502. * Retrieve an array of proposal lines
  503. * @param string $filters Filter on other fields
  504. *
  505. * @return int >0 if OK, <0 if KO
  506. */
  507. public function getLinesArray($filters = '')
  508. {
  509. return $this->fetch_lines($filters);
  510. }
  511. /**
  512. * Add a proposal line into database (linked to product/service or not)
  513. * The parameters are already supposed to be appropriate and with final values to the call
  514. * of this method. Also, for the VAT rate, it must have already been defined
  515. * by whose calling the method get_default_tva (societe_vendeuse, societe_acheteuse, '' product)
  516. * and desc must already have the right value (it's up to the caller to manage multilanguage)
  517. *
  518. * @param string $ref Ref of the value
  519. * @param string $value Value
  520. * @param int $position Position of line
  521. * @param int $notrigger disable line update trigger
  522. * @return int >0 if OK, <0 if KO
  523. */
  524. public function addLine($ref, $value, $position = -1, $notrigger = 0)
  525. {
  526. global $langs, $user;
  527. dol_syslog(__METHOD__ . " id=".$this->id.", ref=".$ref.", value=".$value.", notrigger=".$notrigger);
  528. $error = 0;
  529. // Clean parameters
  530. $this->id = $this->id > 0 ? $this->id : 0;
  531. // Check parameters
  532. if (empty($this->id)) {
  533. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  534. $error++;
  535. }
  536. if ($error) {
  537. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  538. return -1;
  539. }
  540. $this->db->begin();
  541. //Fetch current line from the database and then clone the object and set it in $oldcopy property
  542. $this->line = new ProductAttributeValue($this->db);
  543. // Position to use
  544. $positiontouse = $position;
  545. if ($positiontouse == -1) {
  546. $positionmax = $this->line_max(0);
  547. $positiontouse = $positionmax + 1;
  548. }
  549. $this->line->context = $this->context;
  550. $this->line->fk_product_attribute = $this->id;
  551. $this->line->ref = $ref;
  552. $this->line->value = $value;
  553. $this->line->position = $positiontouse;
  554. $result = $this->line->create($user, $notrigger);
  555. if ($result < 0) {
  556. $this->error = $this->line->error;
  557. $this->errors = $this->line->errors;
  558. $this->db->rollback();
  559. return -1;
  560. } else {
  561. $this->db->commit();
  562. return $this->line->id;
  563. }
  564. }
  565. /**
  566. * Update a line
  567. *
  568. * @param int $lineid Id of line
  569. * @param string $ref Ref of the value
  570. * @param string $value Value
  571. * @param int $notrigger disable line update trigger
  572. * @return int >=0 if OK, <0 if KO
  573. */
  574. public function updateLine($lineid, $ref, $value, $notrigger = 0)
  575. {
  576. global $user;
  577. dol_syslog(__METHOD__ . " lineid=$lineid, ref=$ref, value=$value, notrigger=$notrigger");
  578. // Clean parameters
  579. $lineid = $lineid > 0 ? $lineid : 0;
  580. $this->db->begin();
  581. //Fetch current line from the database and then clone the object and set it in $oldcopy property
  582. $this->line = new ProductAttributeValue($this->db);
  583. $result = $this->line->fetch($lineid);
  584. if ($result > 0) {
  585. $this->line->oldcopy = clone $this->line;
  586. $this->line->context = $this->context;
  587. $this->line->ref = $ref;
  588. $this->line->value = $value;
  589. $result = $this->line->update($user, $notrigger);
  590. }
  591. if ($result < 0) {
  592. $this->error = $this->line->error;
  593. $this->errors = $this->line->errors;
  594. $this->db->rollback();
  595. return -1;
  596. } else {
  597. $this->db->commit();
  598. return $result;
  599. }
  600. }
  601. /**
  602. * Delete a line
  603. *
  604. * @param User $user Object user
  605. * @param int $lineid Id of line to delete
  606. * @param int $notrigger disable line update trigger
  607. * @return int >0 if OK, <0 if KO
  608. */
  609. public function deleteLine(User $user, $lineid, $notrigger = 0)
  610. {
  611. dol_syslog(__METHOD__ . " lineid=$lineid, notrigger=$notrigger");
  612. // Clean parameters
  613. $lineid = $lineid > 0 ? $lineid : 0;
  614. $this->db->begin();
  615. //Fetch current line from the database
  616. $this->line = new ProductAttributeValue($this->db);
  617. $result = $this->line->fetch($lineid);
  618. if ($result > 0) {
  619. $this->line->context = $this->context;
  620. $result = $this->line->delete($user, $notrigger);
  621. }
  622. if ($result < 0) {
  623. $this->error = $this->line->error;
  624. $this->errors = $this->line->errors;
  625. $this->db->rollback();
  626. return -1;
  627. } else {
  628. $this->db->commit();
  629. return $result;
  630. }
  631. }
  632. /**
  633. * Returns the number of values for this attribute
  634. *
  635. * @return int
  636. */
  637. public function countChildValues()
  638. {
  639. global $langs;
  640. $error = 0;
  641. $count = 0;
  642. // Clean parameters
  643. $this->id = $this->id > 0 ? $this->id : 0;
  644. // Check parameters
  645. if (empty($this->id)) {
  646. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  647. $error++;
  648. }
  649. if ($error) {
  650. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  651. return -1;
  652. }
  653. $sql = "SELECT COUNT(*) AS count";
  654. $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element_line;
  655. $sql .= " WHERE " . $this->fk_element . " = " . ((int) $this->id);
  656. dol_syslog(__METHOD__, LOG_DEBUG);
  657. $resql = $this->db->query($sql);
  658. if (!$resql) {
  659. $this->errors[] = "Error " . $this->db->lasterror();
  660. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  661. return -1;
  662. }
  663. if ($obj = $this->db->fetch_object($resql)) {
  664. $count = $obj->count;
  665. }
  666. return $count;
  667. }
  668. /**
  669. * Returns the number of products that are using this attribute
  670. *
  671. * @return int
  672. */
  673. public function countChildProducts()
  674. {
  675. global $langs;
  676. $error = 0;
  677. $count = 0;
  678. // Clean parameters
  679. $this->id = $this->id > 0 ? $this->id : 0;
  680. // Check parameters
  681. if (empty($this->id)) {
  682. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  683. $error++;
  684. }
  685. if ($error) {
  686. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  687. return -1;
  688. }
  689. $sql = "SELECT COUNT(*) AS count";
  690. $sql .= " FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val AS pac2v";
  691. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination AS pac ON pac2v.fk_prod_combination = pac.rowid";
  692. $sql .= " WHERE pac2v.fk_prod_attr = " . ((int) $this->id);
  693. $sql .= " AND pac.entity IN (" . getEntity('product') . ")";
  694. dol_syslog(__METHOD__, LOG_DEBUG);
  695. $resql = $this->db->query($sql);
  696. if (!$resql) {
  697. $this->errors[] = "Error " . $this->db->lasterror();
  698. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  699. return -1;
  700. }
  701. if ($obj = $this->db->fetch_object($resql)) {
  702. $count = $obj->count;
  703. }
  704. return $count;
  705. }
  706. /**
  707. * Test if used by a product
  708. *
  709. * @return int <0 KO, =0 if No, =1 if Yes
  710. */
  711. public function isUsed()
  712. {
  713. global $langs;
  714. $error = 0;
  715. // Clean parameters
  716. $this->id = $this->id > 0 ? $this->id : 0;
  717. // Check parameters
  718. if (empty($this->id)) {
  719. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("TechnicalID"));
  720. $error++;
  721. }
  722. if ($error) {
  723. dol_syslog(__METHOD__ . ' ' . $this->errorsToString(), LOG_ERR);
  724. return -1;
  725. }
  726. $sql = "SELECT COUNT(*) AS nb FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_attr = " . ((int) $this->id);
  727. dol_syslog(__METHOD__, LOG_DEBUG);
  728. $resql = $this->db->query($sql);
  729. if (!$resql) {
  730. $this->errors[] = "Error " . $this->db->lasterror();
  731. return -1;
  732. }
  733. $used = 0;
  734. if ($obj = $this->db->fetch_object($resql)) {
  735. $used = $obj->nb;
  736. }
  737. return $used ? 1 : 0;
  738. }
  739. /**
  740. * Save a new position (field position) for details lines.
  741. * You can choose to set position for lines with already a position or lines without any position defined.
  742. *
  743. * @param boolean $renum True to renum all already ordered lines, false to renum only not already ordered lines.
  744. * @param string $rowidorder ASC or DESC
  745. * @return int <0 if KO, >0 if OK
  746. */
  747. public function attributeOrder($renum = false, $rowidorder = 'ASC')
  748. {
  749. // Count number of attributes to reorder (according to choice $renum)
  750. $nl = 0;
  751. $sql = "SELECT count(rowid) FROM " . MAIN_DB_PREFIX . $this->table_element;
  752. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  753. if (!$renum) {
  754. $sql .= " AND position = 0";
  755. } else {
  756. $sql .= " AND position <> 0";
  757. }
  758. dol_syslog(__METHOD__, LOG_DEBUG);
  759. $resql = $this->db->query($sql);
  760. if ($resql) {
  761. $row = $this->db->fetch_row($resql);
  762. $nl = $row[0];
  763. } else {
  764. dol_print_error($this->db);
  765. }
  766. if ($nl > 0) {
  767. // The goal of this part is to reorder all attributes.
  768. $rows = array();
  769. // We first search all attributes
  770. $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . $this->table_element;
  771. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  772. $sql .= " ORDER BY position ASC, rowid " . $rowidorder;
  773. dol_syslog(__METHOD__ . " search all attributes", LOG_DEBUG);
  774. $resql = $this->db->query($sql);
  775. if ($resql) {
  776. $i = 0;
  777. $num = $this->db->num_rows($resql);
  778. while ($i < $num) {
  779. $row = $this->db->fetch_row($resql);
  780. $rows[] = $row[0]; // Add attributes into array rows
  781. $i++;
  782. }
  783. // Now we set a new number for each attributes
  784. if (!empty($rows)) {
  785. foreach ($rows as $key => $row) {
  786. $this->updatePositionOfAttribute($row, ($key + 1));
  787. }
  788. }
  789. } else {
  790. dol_print_error($this->db);
  791. }
  792. }
  793. return 1;
  794. }
  795. /**
  796. * Update position of line (rang)
  797. *
  798. * @param int $rowid Id of line
  799. * @param int $position Position
  800. * @return int <0 if KO, >0 if OK
  801. */
  802. public function updatePositionOfAttribute($rowid, $position)
  803. {
  804. global $hookmanager;
  805. $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET position = " . ((int) $position);
  806. $sql .= " WHERE rowid = " . ((int) $rowid);
  807. dol_syslog(__METHOD__, LOG_DEBUG);
  808. if (!$this->db->query($sql)) {
  809. dol_print_error($this->db);
  810. return -1;
  811. } else {
  812. $parameters = array('rowid' => $rowid, 'position' => $position);
  813. $action = '';
  814. $reshook = $hookmanager->executeHooks('afterPositionOfAttributeUpdate', $parameters, $this, $action);
  815. return ($reshook >= 0 ? 1 : -1);
  816. }
  817. }
  818. /**
  819. * Get position of attribute
  820. *
  821. * @param int $rowid Id of line
  822. * @return int Value of position in table of attributes
  823. */
  824. public function getPositionOfAttribute($rowid)
  825. {
  826. $sql = "SELECT position FROM " . MAIN_DB_PREFIX . $this->table_element;
  827. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  828. dol_syslog(__METHOD__, LOG_DEBUG);
  829. $resql = $this->db->query($sql);
  830. if ($resql) {
  831. $row = $this->db->fetch_row($resql);
  832. return $row[0];
  833. }
  834. return 0;
  835. }
  836. /**
  837. * Update a attribute to have a higher position
  838. *
  839. * @param int $rowid Id of line
  840. * @return int <0 KO, >0 OK
  841. */
  842. public function attributeMoveUp($rowid)
  843. {
  844. $this->attributeOrder(false, 'ASC');
  845. // Get position of attribute
  846. $position = $this->getPositionOfAttribute($rowid);
  847. // Update position of attribute
  848. $this->updateAttributePositionUp($rowid, $position);
  849. return 1;
  850. }
  851. /**
  852. * Update a attribute to have a lower position
  853. *
  854. * @param int $rowid Id of line
  855. * @return int <0 KO, >0 OK
  856. */
  857. public function attributeMoveDown($rowid)
  858. {
  859. $this->attributeOrder(false, 'ASC');
  860. // Get position of line
  861. $position = $this->getPositionOfAttribute($rowid);
  862. // Get max value for position
  863. $max = $this->getMaxAttributesPosition();
  864. // Update position of attribute
  865. $this->updateAttributePositionDown($rowid, $position, $max);
  866. return 1;
  867. }
  868. /**
  869. * Update position of attribute (up)
  870. *
  871. * @param int $rowid Id of line
  872. * @param int $position Position
  873. * @return void
  874. */
  875. public function updateAttributePositionUp($rowid, $position)
  876. {
  877. if ($position > 1) {
  878. $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET position = " . ((int) $position);
  879. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  880. $sql .= " AND position = " . ((int) ($position - 1));
  881. if ($this->db->query($sql)) {
  882. $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET position = " . ((int) ($position - 1));
  883. $sql .= " WHERE rowid = " . ((int) $rowid);
  884. if (!$this->db->query($sql)) {
  885. dol_print_error($this->db);
  886. }
  887. } else {
  888. dol_print_error($this->db);
  889. }
  890. }
  891. }
  892. /**
  893. * Update position of attribute (down)
  894. *
  895. * @param int $rowid Id of line
  896. * @param int $position Position
  897. * @param int $max Max
  898. * @return void
  899. */
  900. public function updateAttributePositionDown($rowid, $position, $max)
  901. {
  902. if ($position < $max) {
  903. $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET position = " . ((int) $position);
  904. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  905. $sql .= " AND position = " . ((int) ($position + 1));
  906. if ($this->db->query($sql)) {
  907. $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET position = " . ((int) ($position + 1));
  908. $sql .= " WHERE rowid = " . ((int) $rowid);
  909. if (!$this->db->query($sql)) {
  910. dol_print_error($this->db);
  911. }
  912. } else {
  913. dol_print_error($this->db);
  914. }
  915. }
  916. }
  917. /**
  918. * Get max value used for position of attributes
  919. *
  920. * @return int Max value of position in table of attributes
  921. */
  922. public function getMaxAttributesPosition()
  923. {
  924. // Search the last position of attributes
  925. $sql = "SELECT max(position) FROM " . MAIN_DB_PREFIX . $this->table_element;
  926. $sql .= " WHERE entity IN (" . getEntity('product') . ")";
  927. dol_syslog(__METHOD__, LOG_DEBUG);
  928. $resql = $this->db->query($sql);
  929. if ($resql) {
  930. $row = $this->db->fetch_row($resql);
  931. return $row[0];
  932. }
  933. return 0;
  934. }
  935. /**
  936. * Update position of attributes with ajax
  937. *
  938. * @param array $rows Array of rows
  939. * @return void
  940. */
  941. public function attributesAjaxOrder($rows)
  942. {
  943. $num = count($rows);
  944. for ($i = 0; $i < $num; $i++) {
  945. $this->updatePositionOfAttribute($rows[$i], ($i + 1));
  946. }
  947. }
  948. /**
  949. * Return a link to the object card (with optionaly the picto)
  950. *
  951. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  952. * @param string $option On what the link point to ('nolink', ...)
  953. * @param int $notooltip 1=Disable tooltip
  954. * @param string $morecss Add more css on link
  955. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  956. * @return string String with URL
  957. */
  958. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  959. {
  960. global $conf, $langs, $hookmanager;
  961. if (!empty($conf->dol_no_mouse_hover)) {
  962. $notooltip = 1; // Force disable tooltips
  963. }
  964. $result = '';
  965. $label = img_picto('', $this->picto) . ' <u>' . $langs->trans("ProductAttribute") . '</u>';
  966. if (isset($this->status)) {
  967. $label .= ' ' . $this->getLibStatut(5);
  968. }
  969. $label .= '<br>';
  970. $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  971. if (!empty($this->label)) {
  972. $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $this->label;
  973. }
  974. $url = dol_buildpath('/variants/card.php', 1) . '?id=' . $this->id;
  975. if ($option != 'nolink') {
  976. // Add param to save lastsearch_values or not
  977. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  978. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  979. $add_save_lastsearch_values = 1;
  980. }
  981. if ($url && $add_save_lastsearch_values) {
  982. $url .= '&save_lastsearch_values=1';
  983. }
  984. }
  985. $linkclose = '';
  986. if (empty($notooltip)) {
  987. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  988. $label = $langs->trans("ShowProductAttribute");
  989. $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
  990. }
  991. $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
  992. $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"';
  993. } else {
  994. $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
  995. }
  996. if ($option == 'nolink' || empty($url)) {
  997. $linkstart = '<span';
  998. } else {
  999. $linkstart = '<a href="' . $url . '"';
  1000. }
  1001. $linkstart .= $linkclose . '>';
  1002. if ($option == 'nolink' || empty($url)) {
  1003. $linkend = '</span>';
  1004. } else {
  1005. $linkend = '</a>';
  1006. }
  1007. $result .= $linkstart;
  1008. if (empty($this->showphoto_on_popup)) {
  1009. if ($withpicto) {
  1010. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  1011. }
  1012. } else {
  1013. if ($withpicto) {
  1014. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  1015. list($class, $module) = explode('@', $this->picto);
  1016. $upload_dir = $conf->$module->multidir_output[$conf->entity] . "/$class/" . dol_sanitizeFileName($this->ref);
  1017. $filearray = dol_dir_list($upload_dir, "files");
  1018. $filename = $filearray[0]['name'];
  1019. if (!empty($filename)) {
  1020. $pospoint = strpos($filearray[0]['name'], '.');
  1021. $pathtophoto = $class . '/' . $this->ref . '/thumbs/' . substr($filename, 0, $pospoint) . '_mini' . substr($filename, $pospoint);
  1022. if (!getDolGlobalString(strtoupper($module . '_' . $class) . '_FORMATLISTPHOTOSASUSERS')) {
  1023. $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo' . $module . '" alt="No photo" border="0" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $module . '&entity=' . $conf->entity . '&file=' . urlencode($pathtophoto) . '"></div></div>';
  1024. } else {
  1025. $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $module . '&entity=' . $conf->entity . '&file=' . urlencode($pathtophoto) . '"></div>';
  1026. }
  1027. $result .= '</div>';
  1028. } else {
  1029. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  1030. }
  1031. }
  1032. }
  1033. if ($withpicto != 2) {
  1034. $result .= $this->ref;
  1035. }
  1036. $result .= $linkend;
  1037. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  1038. global $action, $hookmanager;
  1039. $hookmanager->initHooks(array('variantsdao'));
  1040. $parameters = array('id' => $this->id, 'getnomurl' => $result);
  1041. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1042. if ($reshook > 0) {
  1043. $result = $hookmanager->resPrint;
  1044. } else {
  1045. $result .= $hookmanager->resPrint;
  1046. }
  1047. return $result;
  1048. }
  1049. /**
  1050. * Return the label of the status
  1051. *
  1052. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1053. * @return string Label of status
  1054. */
  1055. public function getLabelStatus($mode = 0)
  1056. {
  1057. return $this->LibStatut(0, $mode);
  1058. }
  1059. /**
  1060. * Return label of status of product attribute
  1061. *
  1062. * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto, 6=Long label + Picto
  1063. * @return string Label
  1064. */
  1065. public function getLibStatut($mode = 0)
  1066. {
  1067. return $this->LibStatut(0, $mode);
  1068. }
  1069. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1070. /**
  1071. * Return label of a status
  1072. *
  1073. * @param int $status Id status
  1074. * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto, 6=Long label + Picto
  1075. * @return string Label
  1076. */
  1077. public function LibStatut($status, $mode = 1)
  1078. {
  1079. // phpcs:enable
  1080. return '';
  1081. }
  1082. // --------------------
  1083. // TODO: All functions here must be redesigned and moved as they are not business functions but output functions
  1084. // --------------------
  1085. /* This is to show add lines */
  1086. /**
  1087. * Show add free and predefined products/services form
  1088. *
  1089. * @param int $dateSelector 1=Show also date range input fields
  1090. * @param Societe $seller Object thirdparty who sell
  1091. * @param Societe $buyer Object thirdparty who buy
  1092. * @param string $defaulttpldir Directory where to find the template
  1093. * @return void
  1094. */
  1095. public function formAddObjectLine($dateSelector, $seller, $buyer, $defaulttpldir = '/variants/tpl')
  1096. {
  1097. global $conf, $user, $langs, $object, $hookmanager;
  1098. global $form;
  1099. // Output template part (modules that overwrite templates must declare this into descriptor)
  1100. // Use global variables + $dateSelector + $seller and $buyer
  1101. // Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook 'formAddObjectLine'.
  1102. $dirtpls = array_merge($conf->modules_parts['tpl'], array($defaulttpldir));
  1103. foreach ($dirtpls as $module => $reldir) {
  1104. if (!empty($module)) {
  1105. $tpl = dol_buildpath($reldir . '/productattributevalueline_create.tpl.php');
  1106. } else {
  1107. $tpl = DOL_DOCUMENT_ROOT . $reldir . '/productattributevalueline_create.tpl.php';
  1108. }
  1109. if (empty($conf->file->strict_mode)) {
  1110. $res = @include $tpl;
  1111. } else {
  1112. $res = include $tpl; // for debug
  1113. }
  1114. if ($res) {
  1115. break;
  1116. }
  1117. }
  1118. }
  1119. /* This is to show array of line of details */
  1120. /**
  1121. * Return HTML table for object lines
  1122. * TODO Move this into an output class file (htmlline.class.php)
  1123. * If lines are into a template, title must also be into a template
  1124. * But for the moment we don't know if it's possible as we keep a method available on overloaded objects.
  1125. *
  1126. * @param string $action Action code
  1127. * @param string $seller Object of seller third party
  1128. * @param string $buyer Object of buyer third party
  1129. * @param int $selected Object line selected
  1130. * @param int $dateSelector 1=Show also date range input fields
  1131. * @param string $defaulttpldir Directory where to find the template
  1132. * @param int $addcreateline 1=Add create line
  1133. * @return void
  1134. */
  1135. public function printObjectLines($action, $seller, $buyer, $selected = 0, $dateSelector = 0, $defaulttpldir = '/variants/tpl', $addcreateline = 0)
  1136. {
  1137. global $conf, $hookmanager, $langs, $user, $form, $object;
  1138. global $mysoc;
  1139. // TODO We should not use global var for this
  1140. global $disableedit, $disablemove, $disableremove;
  1141. $num = count($this->lines);
  1142. $parameters = array('num' => $num, 'selected' => $selected, 'table_element_line' => $this->table_element_line);
  1143. $reshook = $hookmanager->executeHooks('printObjectLineTitle', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1144. if (empty($reshook)) {
  1145. // Output template part (modules that overwrite templates must declare this into descriptor)
  1146. // Use global variables + $dateSelector + $seller and $buyer
  1147. // Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook.
  1148. $dirtpls = array_merge($conf->modules_parts['tpl'], array($defaulttpldir));
  1149. foreach ($dirtpls as $module => $reldir) {
  1150. if (!empty($module)) {
  1151. $tpl = dol_buildpath($reldir . '/productattributevalueline_title.tpl.php');
  1152. } else {
  1153. $tpl = DOL_DOCUMENT_ROOT . $reldir . '/productattributevalueline_title.tpl.php';
  1154. }
  1155. if (empty($conf->file->strict_mode)) {
  1156. $res = @include $tpl;
  1157. } else {
  1158. $res = include $tpl; // for debug
  1159. }
  1160. if ($res) {
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. if ($addcreateline) {
  1166. // Form to add new line
  1167. if ($action != 'selectlines') {
  1168. if ($action != 'editline') {
  1169. // Add products/services form
  1170. $parameters = array();
  1171. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  1172. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  1173. if (empty($reshook))
  1174. $object->formAddObjectLine(1, $mysoc, $buyer);
  1175. }
  1176. }
  1177. }
  1178. $i = 0;
  1179. print "<!-- begin printObjectLines() -->\n";
  1180. foreach ($this->lines as $line) {
  1181. if (is_object($hookmanager)) { // Old code is commented on preceding line.
  1182. $parameters = array('line' => $line, 'num' => $num, 'i' => $i, 'selected' => $selected, 'table_element_line' => $line->table_element);
  1183. $reshook = $hookmanager->executeHooks('printObjectLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1184. }
  1185. if (empty($reshook)) {
  1186. $this->printObjectLine($action, $line, '', $num, $i, $dateSelector, $seller, $buyer, $selected, null, $defaulttpldir);
  1187. }
  1188. $i++;
  1189. }
  1190. print "<!-- end printObjectLines() -->\n";
  1191. }
  1192. /**
  1193. * Return HTML content of a detail line
  1194. * TODO Move this into an output class file (htmlline.class.php)
  1195. *
  1196. * @param string $action GET/POST action
  1197. * @param CommonObjectLine $line Selected object line to output
  1198. * @param string $var Is it a an odd line (true)
  1199. * @param int $num Number of line (0)
  1200. * @param int $i I
  1201. * @param int $dateSelector 1=Show also date range input fields
  1202. * @param string $seller Object of seller third party
  1203. * @param string $buyer Object of buyer third party
  1204. * @param int $selected Object line selected
  1205. * @param Extrafields $extrafields Object of extrafields
  1206. * @param string $defaulttpldir Directory where to find the template (deprecated)
  1207. * @return void
  1208. */
  1209. public function printObjectLine($action, $line, $var, $num, $i, $dateSelector, $seller, $buyer, $selected = 0, $extrafields = null, $defaulttpldir = '/variants/tpl')
  1210. {
  1211. global $conf, $langs, $user, $object, $hookmanager;
  1212. global $form;
  1213. global $object_rights, $disableedit, $disablemove, $disableremove; // TODO We should not use global var for this !
  1214. $object_rights = $user->rights->variants;
  1215. // Line in view mode
  1216. if ($action != 'editline' || $selected != $line->id) {
  1217. // Output template part (modules that overwrite templates must declare this into descriptor)
  1218. // Use global variables + $dateSelector + $seller and $buyer
  1219. // Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook printObjectLine and printObjectSubLine.
  1220. $dirtpls = array_merge($conf->modules_parts['tpl'], array($defaulttpldir));
  1221. foreach ($dirtpls as $module => $reldir) {
  1222. if (!empty($module)) {
  1223. $tpl = dol_buildpath($reldir . '/productattributevalueline_view.tpl.php');
  1224. } else {
  1225. $tpl = DOL_DOCUMENT_ROOT . $reldir . '/productattributevalueline_view.tpl.php';
  1226. }
  1227. if (empty($conf->file->strict_mode)) {
  1228. $res = @include $tpl;
  1229. } else {
  1230. $res = include $tpl; // for debug
  1231. }
  1232. if ($res) {
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. // Line in update mode
  1238. if ($action == 'editline' && $selected == $line->id) {
  1239. // Output template part (modules that overwrite templates must declare this into descriptor)
  1240. // Use global variables + $dateSelector + $seller and $buyer
  1241. // Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook printObjectLine and printObjectSubLine.
  1242. $dirtpls = array_merge($conf->modules_parts['tpl'], array($defaulttpldir));
  1243. foreach ($dirtpls as $module => $reldir) {
  1244. if (!empty($module)) {
  1245. $tpl = dol_buildpath($reldir . '/productattributevalueline_edit.tpl.php');
  1246. } else {
  1247. $tpl = DOL_DOCUMENT_ROOT . $reldir . '/productattributevalueline_edit.tpl.php';
  1248. }
  1249. if (empty($conf->file->strict_mode)) {
  1250. $res = @include $tpl;
  1251. } else {
  1252. $res = include $tpl; // for debug
  1253. }
  1254. if ($res) {
  1255. break;
  1256. }
  1257. }
  1258. }
  1259. }
  1260. /* This is to show array of line of details of source object */
  1261. }