ProductAttribute.class.php 44 KB

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